【问题标题】:query a column that does not have the same value in another column查询在另一列中没有相同值的列
【发布时间】:2017-01-28 02:03:16
【问题描述】:

我的桌子

ItemCode      ItemName       Total
----------------------------------
A              name1          5
A              name1          5
A              name2          10
B              name1          10 
B              name2          25
B              name1          30
C              name2          5
C              name1          30
C              name1          20

我想显示所有itemcode AB 在列Total 中没有相同的值

我的预期结果

ItemCode          ItemName         Total
----------------------------------------
A                  name1             5
A                  name1             5
B                  name2             25
B                  name1             30 

我已经问过这个问题,但这次我的问题比我上一个问题要清楚得多。我认为 1 解决方案是自我加入,但我无法弄清楚。任何帮助对我来说都意义重大,谢谢!

【问题讨论】:

  • 我还是不太清楚:为什么预期结果中有 ItemCode = 'A' 的行?它们来自完全相等的行,具有相同的总计,而您说“在总计列中没有相同的值”。您的意思是您只想要不存在具有相同 Total 的不同 ItemCode 的行的行吗?
  • @Aleksej 嗨,Aleksej 非常感谢您对我的帮助。我想要的只是获取项目代码“A”和“B”中不具有相同Total 的所有项目名称,但如果它们在相同的项目代码中并且具有相同的总数,我希望它们被包括在内。
  • 你只想要 A 和 B 对还是每一对像 (A,C) (B,C) 等

标签: sql oracle join oracle11g self-join


【解决方案1】:

试试这个查询:

select i.*
from my_table i
where i.ItemCode in ('A','B')
and (select count(*) from my_table t where t.ItemCode in ('A','B') 
and t.ItemCode != i.ItemCode and t.total = i.total) = 0

【讨论】:

    【解决方案2】:

    假设您需要不存在另一行的不同itemCode 具有相同Total 的行,并假设ItemCode 始终不为空,一个简单的解决方案可以是:

    with test(ItemCode, ItemName, Total) as 
    (
        select 'A', 'name1', 5  from dual union all
        select 'A', 'name1', 5  from dual union all
        select 'A', 'name2', 10 from dual union all
        select 'B', 'name1', 10 from dual union all
        select 'B', 'name2', 25 from dual union all
        select 'B', 'name1', 30 from dual union all
        select 'C', 'name2', 5  from dual union all
        select 'C', 'name1', 30 from dual union all
        select 'C', 'name1', 20 from dual
    )
    select *
    from test t1
    where ItemCode in ('A', 'B')
      and not exists (
                       select 1
                       from test t2
                       where t1.total    =  t2.total
                         and t1.itemCode != t2.itemCode
                         and ItemCode in ('A', 'B')
                     )
    

    以下速度更快,但可读性较差:

    select ItemCode, ItemName, Total
    from (
            select ItemCode, ItemName, Total, count(distinct ItemCode) over (partition by Total) as itemCount
            from test
            where ItemCode in ('A', 'B')
         )
    where itemCount = 1  
    

    【讨论】:

      【解决方案3】:
      SELECT A.* FROM (SELECT * FROM MYTABLE WHERE ITEMCODE='A')A
             LEFT JOIN (SELECT * FROM MYTABLE WHERE ITEMCODE='B')B ON
          A.TOTAL=B.TOTAL
      WHERE B.ITEMCODE IS NULL
      
      UNION
      
      SELECT B.* FROM (SELECT * FROM MYTABLE WHERE ITEMCODE='B')B
             LEFT JOIN (SELECT * FROM MYTABLE WHERE ITEMCODE='A')A ON
          B.TOTAL=A.TOTAL
      WHERE A.ITEMCODE IS NULL
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多