这是一个改进的问题,基于我昨天提出的问题,并决定数据透视表对于我需要的东西来说太复杂了。我有以下代码,它显示了每个用户完成了多少票。
根据昨天类似帖子的建议(https://stackoverflow.com/users/2291321/ydaetskcor)建议将其简化为如下工作:
SELECT Names, COUNT(Names)
FROM Table1
GROUP BY Names
尽管试图让我的代码在下面打球,但我仍然在努力弄清楚如何让下面的代码显示列表中每个用户的计数。
下面的代码显示了一长串用户,但由于某种原因,我无法让它显示唯一用户的数量(我知道这是一个相对简单的 sql!):
DECLARE @StartDate DateTime;
DECLARE @EndDate DateTime;
-- Date format: YYYY-MM-DD
SET @StartDate = '2013-11-06 00:00:00'
SET @EndDate = GETDATE() -- Today
SELECT (select Username from Membership where UserId = Ticket.CompletedBy) as TicketStatusChangedBy
FROM Ticket INNER JOIN
TicketStatus ON Ticket.TicketStatusID = TicketStatus.TicketStatusID INNER JOIN
Membership ON Ticket.CheckedInBy = Membership.UserId
WHERE TicketStatus.TicketStatusName = 'Completed' and Ticket.ClosedDate >= @StartDate --(GETDATE() - 1)
and Ticket.ClosedDate <= @EndDate --(GETDATE()-0)
ORDER BY Ticket.CompletedBy ASC, Ticket.ClosedDate ASC
显示以下
Paul
Matt
Matt
Bob
Matt
Paul
Matt
Matt
... (could be many different users)
理想情况下,我希望显示用户在列表中的次数,即
Paul | 2
Matt | 5
Bob | 1