表:emp
id | name | sal
----------------
1 | abc | 100
2 | ha | 200
1 | abc | 100
1 | abc | 100
1 | abc | 100
2 | ha | 200
2 | ha | 200
3 | hai | 400
根据这些数据,我希望在表格中为数据重复或不重复显示输出。
我试过这样:
select
count(*) as status
from
[Test].[dbo].[emp]
group by
[id], [name], [sal]
having
count(*) >= 1
order by
count(*) desc
我得到了这个输出:
status
4
3
1
我不想像上面那样得到输出。
我想要显示输出如下
Status
1
当数据在表格中唯一时,状态显示:0值。
1表示重复数据,o表示唯一记录。请告诉我如何获得单一状态值来解决此问题。
答案 0 :(得分:0)
试试这个,
SELECT Id, Name, Sal, COUNT(*) ,
CASE WHEN COUNT(*) > 1 THEN 1
ELSE 0
END Status
FROM
(
SELECT 1 Id, 'abc' Name, 100 Sal
UNION ALL
SELECT 2, 'ha', 200
UNION ALL
SELECT 1, 'abc', 100
UNION ALL
SELECT 1, 'abc', 100
UNION ALL
SELECT 1, 'abc', 100
UNION ALL
SELECT 2, 'ha', 200
UNION ALL
SELECT 2, 'ha', 200
UNION ALL
SELECT 3, 'hai', 400
) A
GROUP BY Id, Name, Sal
答案 1 :(得分:0)
select case when c>0 then 1 else 0 end as status from(
select count(*) as c from
(select count(*) as cout from loss
group by loss_claim,loss_key
having count(*)>1)as a) b