Edytowane:Cytowana wielkość liter blokuje zapytanie. W wyniku drugiej awarii poniżej widać, że Postgres nie lubi wielkich liter. Ma zwyczaj pisania małymi literami, chyba że wymusisz użycie "
cytaty.
--fails
select a
from
(select 1 as "A") as t;
--fails
select A
from
(select 1 as "A") as t;
--works
select "A"
from
(select 1 as "A") as t;
psql:new.sql:5: ERROR: column "a" does not exist
LINE 1: select a
^
psql:new.sql:10: ERROR: column "a" does not exist
LINE 1: select A
^
A
───
1
(1 row)
select ...
timeloggedToday.date_logged "DATE_LOGGED_TODAY",
-- should be: timeloggedToday."DATE_LOGGED" "DATE_LOGGED_TODAY",
...
LEFT JOIN
(SELECT app_user.lower_user_name,
to_char(startdate, '%Y-%m-%d') AS "DATE_LOGGED",
Sum(timeworked) / 3600 "TIME_IN_HOURS",
startdate - Now() "DIFFERENCE_DAYS"
...
) timeloggedToday ON timeloggedToday.lower_user_name = users.author;
Pomyślałbym o pozbyciu się wielkich liter i cudzysłowów. To po prostu spowoduje bóle głowy. Na marginesie, możesz napisać lewe dołączenie tylko raz:
-- instead of enumerating each possible interval
WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
-- select the interval
now()::date - start_date as diff
-- then build up your columns like:
case(case when diff=0 then date_logged else null end) as date_logged_today
case(case when diff=1 then date_logged else null end) as date_logged_yesterday
-- and so on ...
Możesz też zrobić
CASE
WHEN Ifnull(timeloggedToday.time_in_hours, 0) = 0 THEN 'No Time Logged'
WHEN timeloggedToday.time_in_hours < 3.0 THEN 'User has not logged sufficient time'
ELSE 'user has logged sufficient time'
END "STATUS_TODAY",
w funkcję, aby pozbyć się żmudnej redundancji.