Postgres column does not exist

2020-02-15 06:11发布

Someone else wrote a SQL Server query which worked, and I have the job of porting it over to Postgres.

I don't really know either database beyond the very basic SQL commands (I'm working my way through the tutorial and will continue either way, but I'd like to solve this sooner rather than later).

At any rate, I'm getting the error that in Line 4,

ERROR: column timeloggedtoday.date_logged does not exist

I think that comes from the section around line 89 (marked with **), but I can't figure out what the syntax should be--I've tried copying the WITH/AS sections from the docs, but either I'm still making a mistake or the error is somewhere else. There's probably a lot of repeated/extraneous code below, but I thought it better to include it all as I easily could have introduced other errors as I changed to the Postgres syntax.

EDIT:

If I remove the offending sections, the error changes to

ERROR: column timelogged1daysago.date_logged does not exist

which is the next batch of similar code, leading me to believe that I'm making some syntactical error in naming my temporary table (correct terminology?) but I don't see what's wrong and it's hard to find the relevant section in the docs... Googling 'With' or 'As' is, not surprisingly, unhelpful.

SELECT users.author,
       users.display_name,
       timeloggedToday.date_logged "DATE_LOGGED_TODAY",
       timeloggedToday.time_in_hours "TIME_IN_HOURS_TODAY",
       timeloggedToday.difference_days "DIFFERENCE_DAYS_TODAY",
       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",
       timelogged1daysago.date_logged "DATE_LOGGED_1DAYSAGO",
       timelogged1daysago.time_in_hours "TIME_IN_HOURS_1DAYSAGO",
       timelogged1daysago.difference_days "DIFFERENCE_DAYS_1DAYSAGO",
       CASE
           WHEN Ifnull(timelogged1daysago.time_in_hours, 0) = 0 THEN 'No Time Logged'
           WHEN timelogged1daysago.time_in_hours < 3.0 THEN 'User has not logged sufficient time'
           ELSE 'user has logged sufficient time'
       END "STATUS_1DAYSAGO",
       timelogged2daysago.date_logged "DATE_LOGGED_2DAYSAGO",
       timelogged2daysago.time_in_hours "TIME_IN_HOURS_2DAYSAGO",
       timelogged2daysago.difference_days "DIFFERENCE_DAYS_2DAYSAGO",
       CASE
           WHEN Ifnull(timelogged2daysago.time_in_hours, 0) = 0 THEN 'No Time Logged'
           WHEN timelogged2daysago.time_in_hours < 3.0 THEN 'User has not logged sufficient time'
           ELSE 'user has logged sufficient time'
       END "STATUS_2DAYSAGO",
       timelogged3daysago.date_logged "DATE_LOGGED_3DAYSAGO",
       timelogged3daysago.time_in_hours "TIME_IN_HOURS_3DAYSAGO",
       timelogged3daysago.difference_days "DIFFERENCE_DAYS_3DAYSAGO",
       CASE
           WHEN Ifnull(timelogged3daysago.time_in_hours, 0) = 0 THEN 'No Time Logged'
           WHEN timelogged3daysago.time_in_hours < 3.0 THEN 'User has not logged sufficient time'
           ELSE 'user has logged sufficient time'
       END "STATUS_3DAYSAGO",
       timelogged4daysago.date_logged "DATE_LOGGED_4DAYSAGO",
       timelogged4daysago.time_in_hours "TIME_IN_HOURS_4DAYSAGO",
       timelogged4daysago.difference_days "DIFFERENCE_DAYS_4DAYSAGO",
       CASE
           WHEN Ifnull(timelogged4daysago.time_in_hours, 0) = 0 THEN 'No Time Logged'
           WHEN timelogged4daysago.time_in_hours < 3.0 THEN 'User has not logged sufficient time'
           ELSE 'user has logged sufficient time'
       END "STATUS_4DAYSAGO",
       timelogged5daysago.date_logged "DATE_LOGGED_5DAYSAGO",
       timelogged5daysago.time_in_hours "TIME_IN_HOURS_5DAYSAGO",
       timelogged5daysago.difference_days "DIFFERENCE_DAYS_5DAYSAGO",
       CASE
           WHEN Ifnull(timelogged5daysago.time_in_hours, 0) = 0 THEN 'No Time Logged'
           WHEN timelogged5daysago.time_in_hours < 3.0 THEN 'User has not logged sufficient time'
           ELSE 'user has logged sufficient time'
       END "STATUS_5DAYSAGO",
       timelogged6daysago.date_logged "DATE_LOGGED_6DAYSAGO",
       timelogged6daysago.time_in_hours "TIME_IN_HOURS_6DAYSAGO",
       timelogged6daysago.difference_days "DIFFERENCE_DAYS_6DAYSAGO",
       CASE
           WHEN Ifnull(timelogged6daysago.time_in_hours, 0) = 0 THEN 'No Time Logged'
           WHEN timelogged6daysago.time_in_hours < 3.0 THEN 'User has not logged sufficient time'
           ELSE 'user has logged sufficient time'
       END "STATUS_6DAYSAGO",
       timelogged7daysago.date_logged "DATE_LOGGED_7DAYSAGO",
       timelogged7daysago.time_in_hours "TIME_IN_HOURS_7DAYSAGO",
       timelogged7daysago.difference_days "DIFFERENCE_DAYS_7DAYSAGO",
       CASE
           WHEN Ifnull(timelogged7daysago.time_in_hours, 0) = 0 THEN 'No Time Logged'
           WHEN timelogged7daysago.time_in_hours < 3.0 THEN 'User has not logged sufficient time'
           ELSE 'user has logged sufficient time'
       END "STATUS_7DAYSAGO",
       lastReportedTime.last_time_logged
FROM
  (SELECT lower_child_name AS "author",
          cwd_user.display_name
   FROM cwd_membership
   LEFT JOIN cwd_user ON (cwd_membership.lower_child_name = cwd_user.user_name
                          AND cwd_user.directory_id = cwd_membership.directory_id)
   WHERE lower_parent_name = 'jira-developers'
     AND cwd_membership.directory_id = 10100) users
**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"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
     AND startdate - Now() = INTERVAL '0 days'
   GROUP BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now()),
            worklog.startdate
   ORDER BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now())) timeloggedToday ON timeloggedToday.lower_user_name = users.author**
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"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
     AND startdate - Now() = INTERVAL '-1 days'
   GROUP BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now()),
            worklog.startdate
   ORDER BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now())) timelogged1daysago ON timelogged1daysago.lower_user_name = users.author
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"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
     AND startdate - Now() = INTERVAL '-2 days'
   GROUP BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now()),
            worklog.startdate
   ORDER BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now())) timelogged2daysago ON timelogged2daysago.lower_user_name = users.author
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"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
     AND startdate - Now() = INTERVAL '-3 days'
   GROUP BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now()),
            worklog.startdate
   ORDER BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now())) timelogged3daysago ON timelogged3daysago.lower_user_name = users.author
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"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
     AND startdate - Now() = INTERVAL '-4 days'
   GROUP BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now()),
            worklog.startdate
   ORDER BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now())) timelogged4daysago ON timelogged4daysago.lower_user_name = users.author
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"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
     AND startdate - Now() = INTERVAL '-5 days'
   GROUP BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now()),
            worklog.startdate
   ORDER BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now())) timelogged5daysago ON timelogged5daysago.lower_user_name = users.author
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"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
     AND startdate - Now() = INTERVAL '-6 days'
   GROUP BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now()),
            worklog.startdate
   ORDER BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now())) timelogged6daysago ON timelogged6daysago.lower_user_name = users.author
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"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
     AND startdate - Now() = INTERVAL '-7 days'
   GROUP BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now()),
            worklog.startdate
   ORDER BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now())) timelogged7daysago ON timelogged7daysago.lower_user_name = users.author
LEFT JOIN
  (SELECT app_user.lower_user_name,
          Max(startdate) AS "last_time_logged"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   GROUP BY app_user.lower_user_name) lastReportedTime ON lastReportedTime.lower_user_name = users.author

EDIT: Simpler query

I think I've edited out some of the extraneous stuff in hopes of finding this error. At any rate, this shorter query gives me the same error, at least, so maybe that's a little easier to help me debug:

SELECT users.author,
       users.display_name,
       timeloggedToday.date_logged "DATE_LOGGED_TODAY",
       timeloggedToday.time_in_hours "TIME_IN_HOURS_TODAY",
       timeloggedToday.difference_days "DIFFERENCE_DAYS_TODAY",
       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",
       lastReportedTime.last_time_logged
FROM
  (SELECT lower_child_name AS "author",
          cwd_user.display_name
   FROM cwd_membership
   LEFT JOIN cwd_user ON (cwd_membership.lower_child_name = cwd_user.user_name
                          AND cwd_user.directory_id = cwd_membership.directory_id)
   WHERE lower_parent_name = 'jira-developers'
     AND cwd_membership.directory_id = 10100) users
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"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   WHERE to_char(Now() - INTERVAL '8 days', '%Y-%m-%d') < to_char(startdate, '%Y-%m-%d')
     AND startdate - Now() = INTERVAL '0 days'
   GROUP BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now()),
            worklog.startdate
   ORDER BY app_user.lower_user_name,
            to_char(startdate, '%Y-%m-%d'),
            Date(Now())) timeloggedToday ON timeloggedToday.lower_user_name = users.author

LEFT JOIN
  (SELECT app_user.lower_user_name,
          Max(startdate) AS "last_time_logged"
   FROM worklog
   LEFT JOIN app_user ON worklog.author = app_user.user_key
   GROUP BY app_user.lower_user_name) lastReportedTime ON lastReportedTime.lower_user_name = users.author

1条回答
一夜七次
2楼-- · 2020-02-15 06:47

Edited: The quoted capitalization is tanking the query. In the output of the second failure below you can see Postgres does not like the upper case. It has a habbit of lowercaseing everything unless you force with " quotes.

--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;

I would think about getting rid of the capitalization and quotes. It just going to cause headaches. As a side note, you could write the left join just once by:

-- 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 ...

Also you could make

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",

into a function to get rid of the tedious redundancy.

查看更多
登录 后发表回答