編集済み:引用符で囲まれたキャピタライゼーションは、クエリを無駄にします。以下の2番目の失敗の出力では、Postgresが大文字を好まないことがわかります。 "
で強制しない限り、すべてを小文字にする習慣があります。 引用符。
--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;
キャピタライゼーションと引用符を取り除くことを考えます。それはただ頭痛を引き起こすでしょう。ちなみに、左結合は次の方法で1回だけ書くことができます:
-- 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 ...
また、あなたは作ることができます
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",
面倒な冗長性を取り除くための関数に。