在同一列中显示按功能分组的计数

时间:2018-01-01 12:37:36

标签: sql-server

我正在尝试在SQL查询的同一列中显示日期和计数

select top 5  COUNT(IssueDate), IssueDate 
from employee (Nolock) 
group by CardIssueDate  ORDER BY COUNT(IssueDate) DESC

我需要结果

IssueDate:

2015-09-11 (23)
2015-09-29 (89)
2015-08-20 (78)
2016-06-08 (2)
2015-10-29 (234)

关于如何让它像上述结果一样工作的任何指导?

由于

1 个答案:

答案 0 :(得分:2)

使用+Concat函数来连接结果

旧版本

SELECT TOP 5 CONVERT(VARCHAR(10), IssueDate, 20) + ' ('+ Cast(Count(IssueDate) AS VARCHAR(50))+ ')'
FROM   employee (Nolock)
GROUP  BY CardIssueDate
ORDER  BY Count(IssueDate) DESC 

对于新版本(2012 +)

SELECT TOP 5 Concat(CONVERT(VARCHAR(10), IssueDate, 20), ' (', Count(IssueDate), ')')
FROM   employee (Nolock)
GROUP  BY CardIssueDate
ORDER  BY Count(IssueDate) DESC