显示几个月之间每组每天下降的记录

时间:2015-12-13 22:52:03

标签: sql-server-2008 pivot days

我有两张桌子:

Ticket表:

sale_date, center_id, ticket_id

Center表:

center name, center_id

我需要有关如何使结果看起来像附加图像的帮助:

enter image description here

我试过运行脚本,重新安排并没有给我带来好运。

declare @salefrom      datetime
declare @saleto        datetime

select @salefrom = '2015-10-01 00:00:00'
select @saleto   = '2015-10-31 23:59:59'

SELECT
    DATEPART(MM, t.Sale_Date) [MONTH], 
    DATEPART(DD, t.Sale_Date) [DAY], 
    c.Center_name [centers], 
    COUNT(ticket_id) AS [Tickets]
FROM 
    Ticket t 
INNER JOIN 
    Center c ON c.center_id = t.center_id
WHERE
    t.Sale_Date BETWEEN @salefrom AND @saleto 
    AND t.status <> 1 
GROUP BY 
    DATEPART(MM, t.Sale_Date), DATEPART(DD, t.Sale_Date), c.Center_name
ORDER BY
    DATEPART(MM, t.Sale_Date), DATEPART(DD, t.Sale_Date), c.Center_name

谢谢, lemi

Results i am getting from running the above script

0 个答案:

没有答案