来自查询的SQL数据透视表

时间:2016-11-29 11:47:36

标签: sql sql-server

如何将此查询转换为数据透视

select 
branch,
months,
[Parts Revenue Budget]
from #Temp1 

结果
enter image description here

期望的输出
enter image description here

4 个答案:

答案 0 :(得分:2)

   SELECT  BRANCH,
   SUM(CASE WHEN MONTHS = 'JAN' THEN PARTSREVENUEBUDJET END) AS [JAN],
   SUM(CASE WHEN MONTHS = 'FEB' THEN PARTSREVENUEBUDJET END) AS [FEB],
  SUM(CASE WHEN MONTHS = 'MAR' THEN PARTSREVENUEBUDJET END) AS [MARCH],
   SUM(CASE WHEN MONTHS = 'APR' THEN PARTSREVENUEBUDJET END) AS [APR],
   SUM(CASE WHEN PONITS = 'MAY' THEN  PARTSREVENUEBUDJET END) AS [MAY],
   SUM(CASE WHEN MONTHS = 'JUNE' THEN PARTSREVENUEBUDJET END) AS [JUNE],
   SUM(CASE WHEN MONTHS = 'JULY' THEN PARTSREVENUEBUDJET END) AS [JULY],
   SUM(CASE WHEN MONTHS = 'AUG' THEN PARTSREVENUEBUDJET END) AS [AUG],
   SUM(CASE WHEN MONTHS = 'SEP' THEN PARTSREVENUEBUDJET END) AS [SEP],
   SUM(CASE WHEN MONTHS = 'OCT' THEN PARTSREVENUEBUDJET END) AS [OCT],
   SUM(CASE WHEN MONTHS = 'NOV' THEN PARTSREVENUEBUDJET END) AS [NOV],
   SUM(CASE WHEN MONTHS = 'NOV' THEN PARTSREVENUEBUDJET END) AS [DEC],
           SUM(PARTSREVENUEBUDJET) AS 'GRANDTOTAL'
    FROM TABLE 
    GROUP BY BRANCH

2)

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

select @cols = STUFF((SELECT ',' + QUOTENAME(months) 
                    from #TEMP1
                    group by months
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT branch,' + @cols + ',tiot from 
             (
                select *,sum(BUDGET)over(partition by branch) as tiot
                from #TEMP1
            ) x
            pivot 
            (
                sum(BUDGET)
                for months in (' + @cols + ')
            ) p '

exec(@query);

答案 1 :(得分:2)

您可以使用条件聚合:

SELECT t.branch,
       SUM(CASE WHEN t.months = 'JAN' THEN t.[Parts Revenue Budget] ELSE 0 END) as JAN,
       SUM(CASE WHEN t.months = 'FEB,' THEN t.[Parts Revenue Budget] ELSE 0 END) as FEB
       ... As many as you need
       SUM(t.[Parts Revenue Budget]) as grandtotal
FROM #Temp1
GROUP BY t.branch
UNION ALL
SELECT 'Grand Total',
        SUM(CASE WHEN t.months = 'JAN' THEN t.[Parts Revenue Budget] ELSE 0 END) as JAN,
        SUM(CASE WHEN t.months = 'FEB' THEN t.[Parts Revenue Budget] ELSE 0 END) as FEB,
       ... As many as you need
       SUM(t.[Parts Revenue Budget]) as grandtotal
FROM #Temp1 t

答案 2 :(得分:0)

我尝试如下:

CREATE TABLE #TEMP1(BRANCH VARCHAR(100),MONTHS VARCHAR(100),BUDGET varchar(100))

INSERT INTO #TEMP1 VALUES ('CPD','APR','801375')
INSERT INTO #TEMP1 VALUES ('Sabzi Mandi','APR','1033697')
INSERT INTO #TEMP1 VALUES ('DHA','APR','2119835')
INSERT INTO #TEMP1 VALUES ('Quidabad','APR','2100042')
INSERT INTO #TEMP1 VALUES ('Sukkar','AUG','44377320')
INSERT INTO #TEMP1 VALUES ('Baghbanpura','AUG','2726114')
INSERT INTO #TEMP1 VALUES ('Multan','AUG','6068565')


select *,coalesce(CAST(AUG AS NUMERIC(10,2)),0)+coalesce(CAST(apr AS NUMERIC(10,2)),0) as 'grand total'
from 
(select branch , months ,budget from #temp1) as s
pivot 
(MAX(BUDGET) for months in (APR,AUG)) AS PVT

答案 3 :(得分:0)

 CREATE TABLE TEMP1(BRANCH VARCHAR(100),MONTHS VARCHAR(100),BUDGET FLOAT(126));

INSERT INTO TEMP1 VALUES ('ABC','AUG','1000');
INSERT INTO TEMP1 VALUES ('PQR','APR','2000');
INSERT INTO TEMP1 VALUES ('XYZ','AUG','1000');



SELECT
    BRANCH,
    APR,
    AUG,
    (NVL(APR,'0')+NVL(AUG,'0')) AS GRANDTOTAL
    FROM
    (SELECT
    *
    FROM
      (
        SELECT
          BRANCH,
          MONTHS,
          BUDGET
        FROM
          TEMP1
      )
      pivot ( MIN(BUDGET) FOR MONTHS IN ('APR' AS APR,'AUG' AS AUG)))
      UNION ALL
      SELECT 'GRAND TOTAL' AS BRANCH,SUM(APR) AS APR, SUM(AUG) AS AUG, SUM(GRANDTOTAL) AS GRANDTOTAL 
      FROM 
      (SELECT
    BRANCH,
    APR,
    AUG,
    (NVL(APR,'0')+NVL(AUG,'0')) AS GRANDTOTAL
    FROM
    (SELECT
    *
    FROM
      (
        SELECT
          BRANCH,
          MONTHS,
          BUDGET
        FROM
          TEMP1
      )
      pivot ( MIN(BUDGET) FOR MONTHS IN ('APR' AS APR,'AUG' AS AUG) )));