查询在另一列中没有相同值的列
query a column that does not have the same value in another column
我的Table
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
A
和 B
在 Total
列中不具有相同值的所有内容
我的预期结果
ItemCode ItemName Total
----------------------------------------
A name1 5
A name1 5
B name2 25
B name1 30
我已经问过这个问题了,但是这次我的问题比我上次的问题清楚多了。我认为 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
假设您需要的行不存在具有相同 Total
的不同 itemCode
的另一行,并假设 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
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
我的Table
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
A
和 B
在 Total
我的预期结果
ItemCode ItemName Total
----------------------------------------
A name1 5
A name1 5
B name2 25
B name1 30
我已经问过这个问题了,但是这次我的问题比我上次的问题清楚多了。我认为 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
假设您需要的行不存在具有相同 Total
的不同 itemCode
的另一行,并假设 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
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