I have a fact table which has a timestamp and I want to express the day of the week. I could do something like this:
SELECT CASE WHEN date_part(timestamp,'week') = 0 then 'Monday'
WHEN date_part(timestamp,'week') = 1 then 'Tuestday'...
FROM fact_table
Or I could create a dimensional table like the following:
day_number day_name
0 Monday
1 Tuesday
and then make a JOIN with the fact table:
SELECT day_name
FROM fact_table
LEFT JOIN dm_days
on day_number = date_part(timestamp, 'week')
Which one is better?
What if instead of a date is some indicator (for instance where the traffic comes from: newsletter, SEO, affiliates...) that does not require any function (like date_part) to be obtained? For example, would this be better:
SELECT CASE id_channel WHEN 0 then 'SEO'
WHEN 1 then 'Newsletter'...
FROM fact_table
Than this? SELECT channel_name FROM fact_table LEFT JOIN dm_channels on channel_id = id_channel
CASE date_part(timestamp,'week') WHEN 0 then 'Monday' WHEN 1 then 'Tuestday' ...
. But anycase I prefer 2nd variant - if you need to change something, in 1st var. you need DDL whereas in 2nd - DML only.to_char(the_column, 'Day')