Postgres列不存在 [英] Postgres column does not exist

查看:130
本文介绍了Postgres列不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其他人编写了一个有效的SQL Server查询,我的工作是将其移植到Postgres。

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

除了非常基本的SQL命令外,我真的不了解任何一个数据库(我正在按照本教程的方式进行工作,并且无论哪种方式都将继续进行,但是我想尽快解决这个问题)。

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

无论如何,我在第4行中得到了错误,

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


错误:列timeloggedtoday.date_logged不存在

ERROR: column timeloggedtoday.date_logged does not exist

认为 89(标有**),但是我不知道语法应该是什么-我尝试从文档中复制WITH / AS部分,但是我还是犯错了,或者错误在某处其他。下面可能有很多重复/冗长的代码,但是我认为最好包含所有这些内容,因为当我更改为Postgres语法时,很容易引入其他错误。

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.

编辑:

如果我删除有问题的部分,错误将变为

If I remove the offending sections, the error changes to


错误:列timelogged1daysago.date_logged不存在

ERROR: column timelogged1daysago.date_logged does not exist

这是下一批类似的代码,使我相信我在命名我的临时表时出现语法错误(正确的术语?),但我看不出有什么问题,并且很难在文档中找到相关部分...毫无疑问,谷歌搜索 With或 As是无益的。

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

编辑:更简单的查询

我想我是我们编辑了一些无关紧要的内容,希望能发现此错误。无论如何,这个较短的查询至少会给我同样的错误,所以也许这可以帮助我调试:

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


推荐答案

编辑:
引号引起了查询的麻烦。在下面第二个失败的输出中,您可以看到Postgres不喜欢大写。除非您使用 引号,否则它会小写所有内容。

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

也可以使

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

$将b
$ b

转换为一个函数,以消除乏味的冗余。

into a function to get rid of the tedious redundancy.

这篇关于Postgres列不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆