【问题标题】:Return all record for specific value for which another column value is not max返回另一列值不是最大值的特定值的所有记录
【发布时间】:2023-04-02 03:35:01
【问题描述】:

我有以下输入数据集

nbr,id,qty1,qty2,qty3,qty4,process_timestamp
'1','abc',20,21,22,23,'12/14/2016 9:59'
'1','bcd',20,21,22,23,'12/15/2016 9:59'
'1','bcde',20,21,22,23,'12/13/2016 9:59'
'2','dabc',20,21,22,23,'12/15/2016 9:59'
'2','dabcd',20,21,22,23,'12/16/2016 9:59'

我想要的结果如下

nbr,id,qty1,qty2,qty3,qty4,process_timestamp
'1','bcde',20,21,22,23,'12/13/2016 9:59'
'1','abc',20,21,22,23,'12/14/2016 9:59'
'2','dabc',20,21,22,23,'12/15/2016 9:59'

我想根据字段 nbr 和 process_timestamp 获取所有记录,其中每个 nbr 的 process_timestamp 小于 MAX(process_timestamp)。

这是我迄今为止尝试过的。

Select nbr,id,qty1,qty2,qty3,qty4,process_timestamp from 
check where  process_timestamp NOT IN (SELECT MAX(process_timestamp) from check group by nbr) group by nbr,id,qty1,qty2,qty3,qty4,process_timestamp;

但是我得到的结果(下面)不正确

1       abc     20.0    21.0    22.0    23.0    12/14/2016 9:59
1       bcde    20.0    21.0    22.0    23.0    12/13/2016 9:59

谁能纠正我的错误。

【问题讨论】:

    标签: sql hql not-exists notin


    【解决方案1】:

    在标准 SQL 中,您可以为此使用相关子查询:

    select c.*
    from check c
    where process_timestamp < (select max(c2.process_timestamp)
                               from check c2 
                               where c2.nbr = c.nbr
                              ) ;
    

    【讨论】:

      【解决方案2】:

      试试这个:

      Select nbr,id,qty1,qty2,qty3,qty4,process_timestamp
      from check c1
      where process_timestamp <> (
          SELECT MAX(process_timestamp)
          from check c2
          where c1.nbr = c2.nbr);
      

      【讨论】:

        猜你喜欢
        • 2022-07-16
        • 1970-01-01
        • 1970-01-01
        • 2021-10-16
        • 2020-05-20
        • 1970-01-01
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多