我想从我的表Card
中检索数据。
table Card(
MembershipNumber,
EmbossLine,
status,
EmbossName
)
这样只返回那些具有重复MembershipNumber的行,即count
大于1。
就像我有以下记录
(11,0321,'active','John')
(11,0322,'active','John')
(23,0350,'active','Mary')
(46,0383,'active','Fudge')
(46,0382,'active','Fudge')
(46,0381,'active','Fudge')
查询应返回除第三个以外的所有记录。有可能吗?
已编辑我得到了我的问题的答案。我有另一个问题。我想按status
过滤行,但是当我运行以下查询时,我得不到所需的结果:
SELECT EmbossLine,Membershipnumber,status,embossname,*
FROM (SELECT *,
Count(MembershipNumber)OVER(partition BY EmbossName) AS cnt
FROM card) A
WHERE cnt > 1 AND status='E0'
按状态
添加过滤后答案 0 :(得分:1)
使用Count() Over()
窗口功能执行此操作。
SELECT *
FROM (SELECT *,
Count(MembershipNumber)OVER(partition BY EmbossName) AS cnt
FROM youurtable) A
WHERE cnt > 1
<强>演示强>
SELECT MembershipNumber,
[status],
EmbossName
FROM (SELECT *,
Count(MembershipNumber)OVER(partition BY EmbossName) AS cnt
FROM (VALUES (11.0321,'active','John'),
(11.0322,'active','John'),
(23.0350,'active','Mary'),
(46.0383,'active','Fudge'),
(46.0382,'active','Fudge'),
(46.0381,'active','Fudge')) tc (MembershipNumber, [status], EmbossName)) A
WHERE cnt > 1
答案 1 :(得分:0)
SELECT * FROM CARD a WHERE
(SELECT COUNT(*) FROM CARD b WHERE b.MembershipNumber = a.MembershipNumber) > 1
应该这样做
答案 2 :(得分:0)
Select c.* from card c
join (select MembershipNumber from table group by
MembershipNumber having count(MembershipNumber) > 1) mem
on mem.MembershipNumber = c.MembershipNumber
或者
Select * from table where MembershipNumber in
(select MembershipNumber from table group by
MembershipNumber having count(MembershipNumber) > 1)
答案 3 :(得分:0)
在empbossname上找到重复项并获得结果
select t1.* from card as t1 inner join
(select empbossname from card group by empbossname having count(*)>1) as t2
on t1.empbossname =t2.empbossname