如何透过此特定查询

时间:2016-12-15 17:35:25

标签: sql-server tsql pivot

给出脚本/数据

CREATE TABLE #DemoData
(
    Name varchar(50),
    Billing money,
    Comission money,
    Month varchar(10)
)

insert into #DemoData
VALUES ('Lílian Araújo Albernaz',4308.00,1271.60,'August'),
('Lílian Araújo Albernaz',3254.00,898.75,'December'),
('Lílian Araújo Albernaz',5792.00,1711.89,'July'),
('Lílian Araújo Albernaz',6062.50,1626.18,'November'),
('Lílian Araújo Albernaz',6681.00,1893.06,'October'),
('Lílian Araújo Albernaz',6102.00,1796.34,'September'),
('Leicy Araujo Albernaz',2668.00,2114.92,'August'),
('Leicy Araujo Albernaz',1200.00,950.33,'December'),
('Leicy Araujo Albernaz',3152.50,2498.87,'July'),
('Leicy Araujo Albernaz',2558.00,2030.05,'November'),
('Leicy Araujo Albernaz',2845.00,2251.37,'October'),
('Leicy Araujo Albernaz',3050.00,2417.33,'September'),
('Núbia Nicacio Brito',2309.00,1828.60,'August'),
('Núbia Nicacio Brito',1050.00,828.39,'December'),
('Núbia Nicacio Brito',1977.50,1564.32,'July'),
('Núbia Nicacio Brito',1995.00,1577.34,'November'),
('Núbia Nicacio Brito',2414.00,1908.87,'October'),
('Núbia Nicacio Brito',2575.00,2034.45,'September'),
('Sonia Caxeta',1507.00,1188.02,'August'),
('Sonia Caxeta',900.00,706.99,'December'),
('Sonia Caxeta',1988.00,1571.72,'July'),
('Sonia Caxeta',1655.00,1304.71,'November'),
('Sonia Caxeta',1857.00,1463.44,'October'),
('Sonia Caxeta',2013.00,1587.93,'September'),
('Mirian Pinto Rabelo',1020.00,454.67,'August'),
('Mirian Pinto Rabelo',800.00,359.84,'December'),
('Mirian Pinto Rabelo',1135.00,518.28,'July'),
('Mirian Pinto Rabelo',1000.00,443.42,'November'),
('Mirian Pinto Rabelo',2020.00,911.13,'October'),
('Mirian Pinto Rabelo',1415.00,625.48,'September'),
('Cleuza',2683.00,1277.25,'August'),
('Cleuza',1530.00,688.70,'December'),
('Cleuza',2012.00,984.47,'July'),
('Cleuza',2790.00,1311.74,'November'),
('Cleuza',2298.00,1099.48,'October'),
('Cleuza',2160.00,1048.80,'September'),
('Nivia Santos Caldeira Alves',80.00,38.97,'October'),
('Thais Pereira da Cruz',449.00,357.43,'December'),
('Thais Pereira da Cruz',227.00,179.02,'November'),
('Geisiane Araujo Amparo',305.00,145.03,'December')

如何调整此表以便我得到:

NAME   | Month 1 | Month 2 | ... | Month N
Lilian    100       200              300
Janeth    10        NULL             NULL
Dorias    300       200              NULL

EG:我收到一行开票和一行佣金......

PS:鉴于时间敏感度,我可以处理2个查询,一个用于计费,另一个用于佣金

EDIT1
我到目前为止的查询:

DECLARE @cols AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(MES)
                from #DemoData
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

set @query = 'SELECT Name,' + @cols + ' 
        from 
        #DemoData AS p
        pivot 
        (
            SUM(p.Billing)
            for p.Month in (' + @cols + ')
        ) AS p'
        --GROUP BY Name,'+@cols

 execute sp_executesql @query;

1 个答案:

答案 0 :(得分:2)

这是一个快速支付结算。 (让它变得动态并将结算和佣金作为列或甚至行进行是一件小事)

Select [Name],[Month 01],[Month 02],[Month 03],[Month 04],[Month 05],[Month 06],[Month 07],[Month 08],[Month 09],[Month 10],[Month 11],[Month 12]
  From (
        Select [Name],[Billing],[Col] = Format(MM,'Month 00')
         From  #DemoData A
         Join (Select * From (Values(1,'January'),(2,'February'),(3,'March'),(4,'April'),(5,'May'),(6,'June'),(7,'July'),(8,'August'),(9,'September'),(10,'October'),(11,'November'),(12,'December')) Mths(MM,MMMM)) B
           on  A.Month=B.MMMM
       ) A
 Pivot (Sum([Billing]) For [Col] in ([Month 01],[Month 02],[Month 03],[Month 04],[Month 05],[Month 06],[Month 07],[Month 08],[Month 09],[Month 10],[Month 11],[Month 12]) ) p

返回

enter image description here

编辑 - 动态版

Declare @SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName(Col) 
                                    From (Select Distinct [Col] = Format(MM,'Month 00')
                                          From   #DemoData A
                                          Join (Select * From (Values(1,'January'),(2,'February'),(3,'March'),(4,'April'),(5,'May'),(6,'June'),(7,'July'),(8,'August'),(9,'September'),(10,'October'),(11,'November'),(12,'December')) Mths(MM,MMMM)) B
                                          on A.Month = B.MMMM  
                                         ) A Order by 1 For XML Path('')),1,1,'') 
Select  @SQL = '
 Select [Name],' + @SQL + '
  From (
        Select [Name],[Billing],[Col] = Format(MM,''Month 00'')
         From  #DemoData A
         Join (Select * From (Values(1,''January''),(2,''February''),(3,''March''),(4,''April''),(5,''May''),(6,''June''),(7,''July''),(8,''August''),(9,''September''),(10,''October''),(11,''November''),(12,''December'')) Mths(MM,MMMM)) B
           on  A.Month=B.MMMM
       ) A
 Pivot (Sum([Billing]) For [Col] in (' + @SQL + ') ) p'
Exec(@SQL);

返回

enter image description here