如何显示带有计数的所有列?

时间:2019-08-09 08:58:17

标签: sql-server tsql group-by

在我选择的结果中,我看到两行都可以,但我想查看所有列,如:

'Anne','Bauer','m1'
'Thomas','Neben','m3'
If (OBJECT_ID('tempdb..##test') Is Not Null)
   Begin
      Drop Table ##test
   End

CREATE TABLE ##test (
    givenname varchar(50), 
    surname varchar(50), 
    rann varchar(50)
);


INSERT INTO ##test VALUES ('Anne','Bauer','m1');
INSERT INTO ##test VALUES ('Klaus','Griebe','m2');
INSERT INTO ##test VALUES ('Thomas','Neben','m2');
INSERT INTO ##test VALUES ('Thomas','Neben','m3');
SELECT count(rann) as coun, rann  
FROM ##test
group by rann
having count(rann) = 1
  order by coun desc

2 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

select givenname,surname,rann from (
select *,count(*) over (partition by rann) rn from ##test
)t where rn=1

答案 1 :(得分:0)

只使用带计数的分区

   Select count(rann) over (partition by rann) [count], rann from ##test