【问题标题】:SQL Exclude row based on other rows valueSQL 根据其他行值排除行
【发布时间】:2020-07-23 21:16:46
【问题描述】:
LINE_INVOICE    PAYMENT_SOURCE    SOURCE_PMT_ID                     
-1              Payment Received    7369442                                         
1               Payment Received    7369442                                         
2               Payment Received    7369442                                         
3               Payment Received    7369442                                         
4               Payment Received    7369442                                         
5               Payment Received    7369442                                         
6               Payment Received    7369442                                         
7               Payment Received    7369442                                         
8               Payment Received    7369442                                         
9               Payment Received    7369442                                         
10              Payment Received    7369442                                         
11              Payment Received    7369442                                         
12              Payment Received    7369442                             
                                                

我想在“行发票列”中有任何其他数字时删除“-1”行。这是特定于每个 Source_PMT_ID 的。如果没有'1'行返回-1。如何在我的 sql 查询中捕获它?

【问题讨论】:

  • 在您的示例数据中,您希望删除 LINE_INVOICE 中带有 -1 的行,对吗?

标签: sql oracle where-clause window-functions


【解决方案1】:

怀疑你想要窗口函数:

select *
from (
    select 
        t.*,
        max(case when line_invoice <> -1 then 1 else 0 end) 
            over(partition by source_pmt_id) flag
    from mytable t
) t
where line_invoice <> -1 or flag = 0

子查询中的窗口max() 检查是否存在至少一行具有相同source_pmt_idline_invoice 值而不是-1。外部查询使用该信息过滤掉line_invoice 具有值-1 的行,同时保留那些对于相同source_pmt_id 没有其他值的行。

【讨论】:

  • Oracle SQL 需要这个
  • 我在 Fiddle 中尝试了这个解决方案,它返回给定数据集的 -1。我认为 OP 要求的是在这种情况下删除 -1。
  • @LarsSkaug:我的错,最后一个条件必须是flag = 0。固定。
  • 此解决方案在您的代码中将“line”替换为“line_invoice”后也可以使用。
【解决方案2】:

窗口函数的一种方式:

select t.*
from (select t.*, max(line_invoice) over (partition by SOURCE_PMT_ID) as max_line_invoice
      from t
     ) t
where line_invoice > -1 or max_line_invoice = -1;

或者,没有子查询:

select t.*
from t
order by row_number() over (partition by source_pmt_id
                            order by case when line_invoice <> -1 then 1 else 2 end
                           )
fetch first 1 row with ties;

【讨论】:

    【解决方案3】:

    假设 -1 是最小的数字,并且 line_invoice 从那里上升:

    select * 
    from table1 tab
    where LINE_INVOICE >= case when (select max(line_invoice) 
                                    from table1 tb_
                                    where tb_.payment_source = tab.payment_source
                                     and tb_.source_pmt_id = tab.source_pmt_id) = -1 then -1 else 0 end 
    

    This Fiddle shows how it works.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多