sql 查询:对于 is_queue_empty=1 且queue_name 为空的每条记录,按时间戳获取紧邻的下一条记录,其中 …

发布于 2024-10-24 04:08:55 字数 901 浏览 3 评论 0原文

我们如何构造具有以下约束的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2024-10-31 04:08:55
select a.session_id,a.request_id,a.timestamp,a.queue_tag,
  b.*
from
(
    select session_id,request_id,timestamp,queue_tag,
     (select min(b.timestamp)
      from tbl b
      where a.session_id=b.session_id
        and a.request_id=b.request_id
        and b.timestamp > a.timestamp
        and b.is_queue_empty=0) nextrec
    from tbl a
    where is_queue_empty=1 and nullif(queue_name,'') is null
) a
left join tbl b on a.session_id=b.session_id
               and a.request_id=b.request_id
               and a.nextrec = b.timestamp

注意:

  1. tbl 是表的名称
  2. 假设时间戳在 session_id、request_id 组合中是唯一的
select a.session_id,a.request_id,a.timestamp,a.queue_tag,
  b.*
from
(
    select session_id,request_id,timestamp,queue_tag,
     (select min(b.timestamp)
      from tbl b
      where a.session_id=b.session_id
        and a.request_id=b.request_id
        and b.timestamp > a.timestamp
        and b.is_queue_empty=0) nextrec
    from tbl a
    where is_queue_empty=1 and nullif(queue_name,'') is null
) a
left join tbl b on a.session_id=b.session_id
               and a.request_id=b.request_id
               and a.nextrec = b.timestamp

Note:

  1. tbl is the name of the table
  2. Assuming timestamp is unique within a session_id,request_id combo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文