有人写了一个SQL Server查询,我的工作是将它移植到Postgres。
除了最基本的SQL命令之外,我对任何一个数据库都不太了解(我正在学习教程,并将继续学习这两种方法,但我希望尽早解决这个问题)。
不管怎样,我得到的错误是在第4行,
错误:列为TimelGotoDay.不存在DeaLogyLogd
我认为这来自第89行(标记为**)附近的部分,但我无法确定语法应该是什么——我已经尝试从文档中复制with/AS部分,但要么我仍在犯错误,要么错误在其他地方。下面可能有很多重复的/无关的代码,但是我认为最好包含所有这些代码,因为当我更改到Postgres语法时,我可以很容易地引入其他错误。
编辑:
如果删除有问题的部分,则错误将更改为
错误:列TimelGoGeDaySaGoo.DATEYLoG不存在
下一批类似的代码是什么,这使我相信我在命名临时表时犯了一些语法错误(正确的术语?)但我不知道出了什么问题,很难在文档中找到相关的部分。。。搜索“With”或“As”并不奇怪,没有帮助。

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

编辑:更简单的查询
我想我已经删掉了一些无关的东西,希望能找到这个错误。无论如何,这个较短的查询至少会给我同样的错误,所以这可能更容易帮助我调试:
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不喜欢大写。它有一个小写的habbit,除非您强制使用"引号。

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

我会考虑去掉大写和引号。只会引起头痛。作为补充说明,您可以只通过以下方式编写左连接:
-- 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",

变成一个函数来摆脱繁琐的冗余。

关于sql - Postgres列不存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25814856/

10-11 06:59