如何赛筛选出多列内容相同的数据??
在MSSQL里,如何篩選出多個列的內(nèi)容相同的數(shù)據(jù),如:
ID??? Name??? Desc.??? Unit??? QTY
001?? ABC???? 15*20??? PCS???? 15
001?? ABC???? 15*20??? PCS???? 16
001?? ABC???? 15*30??? PCS???? 10
002?? DEE???? 21/25??? PCS???? 110
002?? DEE???? 21/25??? PCS???? 10
002?? DEE???? 18/25??? PCS???? 110
要把 ID,Name, Desc. 內(nèi)容都相同的數(shù)據(jù)選擇出來。語句要怎么寫?
create table t1
(
ID? varchar(10),
Name?? varchar(10),
[Desc.]?? varchar(10),
Unit??? varchar(10),
QTY?? int
)
insert into t1 select '001',?? 'ABC',???? '15*20',??? 'PCS',???? 15
union all select '001',?? 'ABC',???? '15*20',??? 'PCS',???? 16
union all select '001',?? 'ABC',???? '15*30',??? 'PCS',???? 10
union all select '002',?? 'DEE',???? '21/25',??? 'PCS',???? 110
union all select '002',?? 'DEE',???? '21/25',??? 'PCS',???? 10
union all select '002',?? 'DEE',???? '18/25',??? 'PCS',???? 110
查詢:
select * from t1 as a where (select count(1) from t1 where [id]=a.[id] and [name]=a.[name]
?and [Desc.]=a.[Desc.])>1
結(jié)果:
select? ID,Name, [Desc.]
from a
group by? ID,Name, [Desc.]
having count(Unit)>1
ID???????? Name?????? Desc.?????
---------- ---------- ----------
001??????? ABC??????? 15*20
002??????? DEE??????? 21/25
(所影響的行數(shù)為 2 行)
轉(zhuǎn)載于:https://www.cnblogs.com/martian6125/archive/2009/07/23/9631280.html
總結(jié)
以上是生活随笔為你收集整理的如何赛筛选出多列内容相同的数据??的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 南阳里程
- 下一篇: rs.open select * fro