时间:2021-07-01 10:21:17 帮助过:24人阅读
下面是语句是用来统计同名同姓人的个数。非常不错。需要的朋友可以参考下。
sql语句查询
SELECT * FROM (SELECT DISTINCT Name,Count(ID) as 人員個數,
sum(case when Sex='男' then 1 else 0 end) as 男人數,
sum(case when Sex='女' then 1 else 0 end) as 女人數
FROM Test group by Name) A
OUTER APPLY(
SELECT
[包含ID]= STUFF(REPLACE(REPLACE(
(
SELECT ID FROM Test N
WHERE Name = A.Name
FOR XML AUTO
), '', ''), 1, 1, '')
)N