从SQL生成类似Excel的报告

时间:2016-11-10 22:27:55

标签: sql-server

我是SQL和新手 我有两个表Ticket和TicketAttributes以及以下Schema

CREATE TABLE [dbo].[Ticket](
   [TicketID] [int] IDENTITY(1,1) NOT NULL, --Primary key
   [Category] [varchar](256) NOT NULL,
   [Description] [varchar](256) NULL,
   [LibID] [int] NOT NULL,
   [Status] [smallint] NULL,
   [LogID] [int] NULL)

故障单属性

CREATE TABLE [dbo].[TicketAttributes](
    [TicketID] [int] NOT NULL,
    [TicketAttrID] [int] IDENTITY(1,1) NOT NULL,
    [AttributeID] [int] NOT NULL,
    [AttributeGroup] [varchar](255) NULL,
    [AttributeValue] [nvarchar](max) NULL,
    [Status] [smallint] NULL,
    [LogID] [int] NULL)

其中Ticket Attribute是另一个存储TicketStatus,TicketCategory等票证的不同属性的表。 现在我需要生成一个看起来像

的报告
                   TicketStatus1     TicketStatus 2     TicketStatus3
    -----------------------------------------------------------------
TicketCategory1         7                3
Ticketcategory2                          4
TicketCategory3                                            8

我想查看每个故障单类别的每个状态的计数。 例如: - 我在TicketTable中有以下数据

----------------------------------------------
TicketID  Name         Price        Date
------------------------------------------------
155       Ticket4      $20     16 Jan 2016
157       Ticket3      $300    17 Jan 2016
158       Ticket1      $100    18 Jan 2016
159       Ticket2      $500    19 Jan 2016

现在在TicketAttribute表中

----------------------------------------------
    TicketID  AttributeID  AttributeValue
    ------------------------------------------------
    155       500             Joe     
    155       600             Reserved    
    155       700             Economy
    155       800             San Jose   

其中AttributeIDs

 500=Nameofthe Passenger
 600= Status of Ticket
 700= Class
 800= Destination

现在我想说看看每个班级每个状态的活动门票数量是多少

               Booked     Cancelled     PaymentPending  ............
    -----------------------------------------------------------------
Economy         7                3
Economy Plus                     4
Business                                         8

希望我现在很清楚。 如何使用SQL Query

进行此操作

1 个答案:

答案 0 :(得分:2)

使用PIVOT

;WITH cte AS (
    SELECT
       c.AttributeValue as Class
       ,s.AttributeValue as StatusOfTicket
    FROM
       Ticket t
       LEFT JOIN TicketAttributes c
       ON t.TicketId = c.TicketId
       AND c.AttributeID = 700
       LEFT JOIN TicketAttributes s
       ON t.TicketId = s.TicketId
       AND s.AttributeID = 600
)

SELECT *
FROM
    cte
    PIVOT (
       COUNT(StatusOfTicket) FOR StatusOfTicket IN (Reserved,Cancelled,PaymentPending)
    ) p

使用条件聚合:

SELECT
    c.AttributeValue as Class
    ,COUNT(DISTINCT CASE WHEN s.AttributeValue = 'Reserved' THEN c.TicketId END) as Reserved
    ,COUNT(DISTINCT CASE WHEN s.AttributeValue = 'Cancelled' THEN c.TicketId END) as Cancelled
    ,COUNT(DISTINCT CASE WHEN s.AttributeValue = 'PaymentPending' THEN c.TicketId END) as PaymentPending
FROM
    Ticket t
    LEFT JOIN TicketAttributes c
    ON t.TicketId = c.TicketId
    AND c.AttributeID = 700
    LEFT JOIN TicketAttributes s
    ON t.TicketId = s.TicketId
    AND s.AttributeID = 600
GROUP BY
    c.AttributeValue