Sample data:
dateHour |
---|
2021-08-01 18:00:00.000 |
2021-08-02 20:00:00.000 |
2021-08-03 06:00:00.000 |
2021-08-04 08:00:00.000 |
2021-08-05 09:00:00.000 |
Trying to build an aggregate table at the weekday level. So there is a need to extract full day of week name from the dateHour
column. dateHour
column contains date and hour value of an event in it. It is like a timestamp column.
One way of extracting the full day name is using the below case when query. This solution works but it is taking a lot of time due to case statements. This is creating a bottle neck on the performance of the query.
select
case dayname("dateHour"::date)
when 'Mon' then 'Monday'
when 'Tue' then 'Tuesday'
when 'Wed' then 'Wednesday'
when 'Thu' then 'Thursday'
when 'Fri' then 'Friday'
when 'Sat' then 'Saturday'
when 'Sun' then 'Sunday'
end as "day_of_week"
from tableName
Another query that I have tried is:
select TO_CHAR(CURRENT_DATE, 'DYDY') Day_Full_Name;
The above query works fine when CURRENT_DATE
is used but when CURRENT_DATE
is replaced with the column name dateHour
from the table then it is giving short week day twice.
Something like this:
SunSun
MonMon
TueTue
WedWed
ThuThu
To replicate the DYDY issue check with the below code snippets:
with cte as (
select '2021-08-01 18:00:00.000'::timestamp as "dateHour"
)
select
"dateHour"::date as dt,
TO_CHAR(dt,'DYDY') day_full_name
from cte ;
The output from the above query:
DT | DAY_FULL_NAME |
---|---|
2021-08-01 | Sunday |
with cte as (
select '2021-08-01 18:00:00.000'::timestamp as "dateHour"
union all
select '2021-08-02 20:00:00.000'::timestamp as "dateHour"
)
select
"dateHour"::date as dt,
TO_CHAR(dt,'DYDY') day_full_name
from cte
;
Output:
DT | DAY_FULL_NAME |
---|---|
2021-08-01 | SunSun |
2021-08-02 | SunMon |
Expected output (In the Output Sunday can be either of these: Sunday / SUNDAY )
fullDayofWeekName |
---|
SUNDAY |
MONDAY |
TUESDAY |
WEDNESDAY |
THURSDAY |
I need an efficient way to generate the full week day name from the dateHour
column. It shouldn't effect the performance of the aggregate query.