【问题标题】:SQL check if group containes certain values of given column (ORACLE)SQL检查组是否包含给定列的某些值(ORACLE)
【发布时间】:2017-06-20 13:02:02
【问题描述】:

我有包含这些记录的表 audit_log:

log_id | request_id | status_id
 1     |  2         |  5
 2     |  2         |  10
 3     |  2         |  20
 4     |  3         |  10
 5     |  3         |  20

我想知道是否存在同时具有 status_id 5 和 10 的 request_ids。因此,此查询应返回 request_id = 2,因为它的列 status_id 具有值 5 和 10(省略了 request_id 3,因为 status_id 列只有值 10 而没有 5)。
我怎么能用 SQL 做到这一点?
我想我应该按 request_id 使用 group,但我不知道如何检查 group 是否具有值为 5 和 10 的 status_id?

谢谢,
错别字

【问题讨论】:

  • 你可以使用GROUP_CONCAT
  • 我正在使用 Oralce 数据库...
  • request_id/status_id 的组合是唯一的吗?

标签: sql oracle group-by


【解决方案1】:

这可能是一种方式:

/* input data */ 
with yourTable(log_id , request_id , status_id) as (
 select 1   , 2     ,  5 from dual union all
 select 2   , 2     , 10 from dual union all
 select 3   , 2     , 20 from dual union all
 select 4   , 3     , 10 from dual union all
 select 5   , 3     , 20 from dual
 )
/* query */
select request_id
from yourTable
group by request_id
having count( distinct case when status_id in (5,10) then status_id end) = 2 

它是如何工作的:

select request_id, 
       case when status_id in (5,10) then status_id end as checkColumn
from yourTable

给予

REQUEST_ID CHECKCOLUMN
---------- -----------
         2           5
         2          10
         2
         3          10
         3

所以条件count (distinct ...) = 2 起作用了

【讨论】:

    【解决方案2】:
    SELECT   request_id
    FROM     table_name
    GROUP BY request_id
    HAVING   COUNT( CASE status_id WHEN  5 THEN 1 END ) > 0
    AND      COUNT( CASE status_id WHEN 10 THEN 1 END ) > 0
    

    【讨论】:

      【解决方案3】:

      要检查两个值是否存在(不考虑其他值),您可以在聚合之前进行过滤:

      select request_id
      from yourTable
      where status_id in (5,10)
      group by request_id
      having count(*) = 2 -- status_id is unique
       -- or 
      having count(distinct status_id) = 2 -- status_id exists multiple times
      

      【讨论】:

      • 经过大量搜索,您救了我的命!谢谢!
      【解决方案4】:

      应该这样做:

      select
          log5.*, log10.status_id
      from
          audit_log log5
          join audit_log log10 on log10.request_id = log5.request_id
      where
          log5.status_id = 5
          and log10.status_id = 10
      order by
          log5.request_id
      ;
      

      这是输出:

      + ----------- + --------------- + -------------- + -------------- +
      | log_id      | request_id      | status_id      | status_id      |
      + ----------- + --------------- + -------------- + -------------- +
      | 1           | 2               | 5              | 10             |
      + ----------- + --------------- + -------------- + -------------- +
      1 rows
      

      这是设置示例的 sql:

      create table audit_log (
          log_id int,
          request_id int,
          status_id int
       );
      
      insert into audit_log values (1,2,5);
      insert into audit_log values (2,2,10);
      insert into audit_log values (3,2,20);
      insert into audit_log values (4,3,10);
      insert into audit_log values (5,3,20);
      

      【讨论】:

        猜你喜欢
        • 2012-11-27
        • 2014-05-26
        • 1970-01-01
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-08
        相关资源
        最近更新 更多