1

我有以下代码,它向我显示了哪些用户已完成票证,并列出了每个用户以及他们关闭票证的日期。IE

Paul
Matt
Matt
Bob
Matt
Paul
Matt
Matt

目前,我自己手动计算每个用户,以查看他们当天的总数。

编辑:将输出更改为列而不是行:

我一直在尝试做的是让 SQL Server 为我执行此操作,即最终结果如下所示:

Paul  |  2
Matt  |  5
Bob   |  1

我目前使用的代码是,如果有人可以帮助我改变它,我会很高兴,这样我就可以让它输出类似于上面的东西?

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

感谢您的帮助和时间。

4

3 回答 3

1

不确定你是否特别想要这种格式,但如果你有它在行中,它作为查询要简单得多:

SELECT Names, COUNT(Names)
FROM Table1
GROUP BY Names

SQL小提琴

于 2013-11-07T11:18:55.947 回答
0
CREATE TABLE  SomeTable 
(Name NVARCHAR(20))
GO

INSERT INTO SomeTable 
VALUES ('Mark'), ('Mark'),('Mark'),('SAM'),('SAM'),('Josh')
GO

;WITH CTE
AS
(
    SELECT NAME, COUNT(*) AS Total FROM SomeTable
    GROUP BY Name
)

SELECT * 
FROM CTE
        PIVOT (
                MAX(Total)
                FOR NAME 
                IN ([Mark], [SAM], [Josh])      
               )l

结果集

Mark SAM  Josh
3    2    1

显然,如果表中有很多值,您将需要使用动态 Sql,首先构建所有不同值/名称的逗号分隔列表,然后将上述查询的“IN”子句中的名称列表传递给动态 Sql .
使用动态 SQL

DECLARE @NameList NVARCHAR(MAX) = ''
DECLARE @Sql NVARCHAR(MAX)

SELECT @NameList =   @NameList + ',['  + Name + ']' 
                    FROM (SELECT DISTINCT NAME FROM SomeTable)t
SET @NameList =    STUFF(@NameList, 1, 1, '')
PRINT @NameList      --<---- for checking purpose to see if list is as expected

SET @Sql = ';WITH CTE
AS
(
    SELECT NAME, COUNT(*) AS Total FROM SomeTable
    GROUP BY Name
)

SELECT * 
FROM CTE
        PIVOT (
                MAX(Total)
                FOR NAME 
                IN ('+ @NameList + ')       
               )l'
EXECUTE sp_executesql @Sql

结果集

Mark SAM  Josh
3    2    1
于 2013-11-07T11:52:45.023 回答
0
Declare @t table(names varchar(100))

DECLARE @StartDate DateTime;
DECLARE @EndDate DateTime;

-- Date format: YYYY-MM-DD
SET @StartDate = '2013-11-06 00:00:00'
SET @EndDate = GETDATE()  -- Today

insert into @t(names)
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

Select 
sum(case when names='Paul' then 1 else 0 end) as Paul,
sum(case when names='Matt' then 1 else 0 end) as Matt,
.
.
.
from @t
于 2013-11-07T11:53:40.920 回答