基本上,我的SQL代码正在尝试获取在下午5:15之后工作的人数所占的百分比。这两个表代表一个在下午5:15之后停留的表,另一个在5:15之前离开的表。以下是我的查询:
WITH people_that_left_early AS (
SELECT username, MAX(created_at) AS timestamp
FROM doorlogs
WHERE created_at <= '2017-07-03 05:15:00 PM'
AND created_at >= '2017-07-03 06:00:00 AM'
GROUP BY username),
WITH people_that_stayed_late AS (
SELECT a.username, a.event, b.timestamp
FROM doorlogs a
INNER JOIN people_that_left_early b
ON a.username = b.username
WHERE a.created_at = b.timestamp
AND event <> 'X')
SELECT COUNT(a.username), COUNT(b.username)
FROM people_that_left_early a
LEFT JOIN people_that_stayed_late b
ON a.username = b.username;
第二张表people_that_stayed_late
基本上是2017年7月3日下午5:15之后没有离开建筑物的人。每次我运行查询时,都会收到错误消息:
ERROR: syntax error at or near "WITH"
LINE 7: WITH people_that_stayed_late AS (
我知道我正在使用第二个表中的第一个表,但是我不明白为什么它会给我这个错误。另外,我也没有进行百分比计算,因为我想看看我是否能找到在那一天去上班的人以及谁在2017年7月3日下午5:15之后停留的正确人数。>
答案 0 :(得分:1)
第二次省略WITH
关键字。
语法是
WITH [ RECURSIVE ] name1 AS (...),
name2 AS (...)
[ , ... ]
SELECT ...