? because week " />
L
L
LakeForest2022-02-14 14:17:22
PostgreSQL
LakeForest, 2022-02-14 14:17:22

Why does SELECT CASE WHEN only return the first value?

Tell me, please, what's wrong? Translates into Russian only the first value - the rest skips.
And it will turn out to group by week, writing `GROUP BY created_date;`? because week does not accept ...

SELECT
    AVG(value) AS avg_value,
    CASE LOWER(TO_CHAR(created_date, 'Day'))
        WHEN 'monday' THEN 'Понедельник'
        WHEN 'tuesday' THEN 'Вторник'
        WHEN 'wednesday' THEN 'Среда'
        WHEN 'thursday' THEN 'Четверг'
        WHEN 'friday' THEN 'Пятница'
        WHEN 'saturday' THEN 'Суббота'
        WHEN 'sunday' THEN 'Воскресенье'
    END week
FROM results
GROUP BY created_date;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2022-02-14
@LakeForest

But if you look at the documentation ...

Day full capitalized day name (blank-padded to 9 chars)

It's really not clear why the comparison of two different strings works like that.
'monday' = 'monday '
What was asked from to_char is quite expected and received. Moreover, what is the point of getting the name of the day of the week, and not its number? What if Day turns out to be a locale-specific thing?

I
idShura, 2022-02-14
@idShura

SELECT
    AVG(value) AS avg_value,
    CASE WHEN  LOWER(TO_CHAR(created_date, 'Day')) = 'monday' THEN 'Понедельник'
         WHEN  LOWER(TO_CHAR(created_date, 'Day')) = 'tuesday' THEN 'Вторник'
         WHEN  LOWER(TO_CHAR(created_date, 'Day')) = 'wednesday' THEN 'Среда'
         WHEN  LOWER(TO_CHAR(created_date, 'Day')) = 'thursday' THEN 'Четверг'
         WHEN  LOWER(TO_CHAR(created_date, 'Day')) = 'friday' THEN 'Пятница'
         WHEN  LOWER(TO_CHAR(created_date, 'Day')) = 'saturday' THEN 'Суббота'
         WHEN  LOWER(TO_CHAR(created_date, 'Day')) = 'sunday' THEN 'Воскресенье'
    END week
FROM results
GROUP BY created_date;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question