sql 查询:对于 is_queue_empty=1 且queue_name 为空的每条记录,按时间戳获取紧邻的下一条记录,其中 …
我们如何构造具有以下约束的 SQL 查询。
对于 is_queue_empty=1 且queue_name 为空的每条记录,按时间戳获取紧邻的下一条记录,其中is_queue_empty=0 且queue_name 对于相同的session-id 和request-id 可以为空,也可以为空。
表具有以下列:
session_id、request_id、queue_name、is_queue_empty、timestamp、queue_tag、tab_name。
到目前为止我所做的是不正确的:
SELECT x.tab_name,
x.is_queue_empty,
x.SESSION_ID,
x.request_ID,
x.TO_CHAR(DATETIME, 'YYYY/MM/DD HH24:MI:SS') timestamp,
y.tab_name,y.queue_name,y.is_queue_empty
FROM queue_data AS x
WHERE
timesttamp < TO_DATE('2011/02/30')
AND timestamp >= TO_DATE('2011/01/01')
AND is_queue_empty=1
AND timestamp < (select TO_CHAR(timestamp, 'YYYY/MM/DD HH24:MI:SS') as timestamp from queue_data as Y where x.session_id = y.session_id and x.request_id=y.request_id and y.is_queue_empty=0 order by y.timestamp asc limit 1 )
How can we construct sql query with following constraint.
For each record where is_queue_empty=1 and queue_name is empty get immediate next record by timestamp where is_queue_empty=0 and queue_name can or cannot be empty for same session-id and request-id.
Table has following columns:
session_id,request_id,queue_name ,is_queue_empty,timestamp,queue_tag,tab_name.
What I have do far is this which is incorrect:
SELECT x.tab_name,
x.is_queue_empty,
x.SESSION_ID,
x.request_ID,
x.TO_CHAR(DATETIME, 'YYYY/MM/DD HH24:MI:SS') timestamp,
y.tab_name,y.queue_name,y.is_queue_empty
FROM queue_data AS x
WHERE
timesttamp < TO_DATE('2011/02/30')
AND timestamp >= TO_DATE('2011/01/01')
AND is_queue_empty=1
AND timestamp < (select TO_CHAR(timestamp, 'YYYY/MM/DD HH24:MI:SS') as timestamp from queue_data as Y where x.session_id = y.session_id and x.request_id=y.request_id and y.is_queue_empty=0 order by y.timestamp asc limit 1 )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注意:
tbl
是表的名称Note:
tbl
is the name of the table