sql server

时间:2015-09-01 06:43:34

标签: sql-server pivot

以下是我的sql server的查询:

     SELECT t.type ,
        t.fmsales ,
        t.indussales 
 FROM   ( SELECT    CASE WHEN ( GROUPING(mti.type) = 1 ) THEN 'ALL'
                         ELSE ISNULL(mti.Type, 'UNKNOWN')
                    END AS type ,
                    SUM(ISNULL(mti.SALES_THIS + mti.SWITCHIN_THIS, 0)) fmsales ,
                    SUM(ISNULL(mti.SALES_ALL + mti.SWITCHIN_ALL, 0)) indussales 

          FROM      dbo.IMonthly_trigger_Industry mti ,
                    dbo.RMMappingDb rm ,
                    dbo.EmployeeDB ed
          WHERE     ed.EmployeeCode = 1217
                    AND rm.BranchName = mti.Branch
                    AND rm.ARNCode = mti.BROKER
                    AND rm.EmployeeCode = ed.EmployeeCode
                    AND mti.BROKER_NAME LIKE '%Pvt Limited%'
          GROUP BY  mti.Type
                    WITH CUBE
        ) t
 WHERE  t.type IS NOT NULL
 GROUP BY t.type ,
        t.fmsales ,
        t.indussales;

,结果如下:

enter image description here

我想转置此结果集,以便行成为列,列成为行。我之前使用了pivot方法,我只有一列来进行聚合,但这里有不同之处。有人可以帮助我实现这一目标。

结果应该是这样的:

enter image description here

我怎样才能做到这一点?我尝试使用pivot,因为pivot只允许聚合在一列上,我无法按预期得到结果。任何帮助深表感谢。提前谢谢。

4 个答案:

答案 0 :(得分:2)

我们可以在交叉申请中完成,但现在已经很晚了

DECLARE @t TABLE(type NVARCHAR(MAX), fmsales MONEY, indussales MONEY)

    INSERT INTO @t VALUES
    ('ALL', 0, 82.1),
    ('CASH', 0, 0),
    ('DEBT', 0, 62),
    ('EQUITY', 0, 20.1)

    select  col As Products,[ALL],[Cash],[Debt],[Equity],
    from (select type,col,val from @t

    CROSS APPLY (values(fmsales, 'fmsales'),( indussales, 'indussales'))CS(col,val))K

    PIVOT(MAX(val)
    FOR TYPE IN
     ([ALL],[Cash],[Debt],[Equity]))P

答案 1 :(得分:1)

您可以使用以下PIVOTUNPIVOT的组合执行此操作:

select * 
from
  (
      select * from t
  ) s
unpivot
  (
    val for product in ([fmsales],[indussales])
  )up
pivot
  (
    max(val) for type in ([ALL],[CASH],[DEBIT],[EQUITY])
   )p

演示sql小提琴链接:http://sqlfiddle.com/#!6/381f7/4

<强>解释

首先,我们使用UNPIVOTfmsalesindussales列名称作为行数据,然后PIVOT作为type列。 在此处查看UNPIVOT的中间结果:

http://sqlfiddle.com/#!6/381f7/5

答案 2 :(得分:1)

您可以在首先unpivoting数据时使用标准技巧,然后pivoting结果:

DECLARE @t TABLE(type NVARCHAR(MAX), fmsales MONEY, indussales MONEY)

INSERT INTO @t VALUES
('ALL', 0, 82.1),
('CASH', 0, 0),
('DEBT', 0, 62),
('EQUITY', 0, 20.1)


SELECT * FROM @t
UNPIVOT(a FOR products IN([fmsales],[indussales]))u
PIVOT (MAX(a) FOR type IN([ALL],[CASH],[DEBT],[EQUITY]))p

输出:

products    ALL     CASH    DEBT    EQUITY
fmsales     0.00    0.00    0.00    0.00
indussales  82.10   0.00    62.00   20.10

要将此应用于您的查询,您可以使用common table expressions(CTE),如:

;WITH cte AS(
             SELECT t.type ,
                    t.fmsales ,
                    t.indussales
             FROM   ( SELECT    CASE WHEN ( GROUPING(mti.type) = 1 ) THEN 'ALL'
                                     ELSE ISNULL(mti.Type, 'UNKNOWN')
                                END AS type ,
                                SUM(ISNULL(mti.SALES_THIS + mti.SWITCHIN_THIS, 0)) fmsales ,
                                SUM(ISNULL(mti.SALES_ALL + mti.SWITCHIN_ALL, 0)) indussales
                      FROM      dbo.IMonthly_trigger_Industry mti ,
                                dbo.RMMappingDb rm ,
                                dbo.EmployeeDB ed
                      WHERE     ed.EmployeeCode = 1217
                                AND rm.BranchName = mti.Branch
                                AND rm.ARNCode = mti.BROKER
                                AND rm.EmployeeCode = ed.EmployeeCode
                                AND mti.BROKER_NAME LIKE '%Pvt Limited%'
                      GROUP BY  mti.Type
                                WITH CUBE
                    ) t
             WHERE  t.type IS NOT NULL
             GROUP BY t.type ,
                    t.fmsales ,
                    t.indussales
        )
SELECT * FROM cte
UNPIVOT(a FOR products IN([fmsales],[indussales]))u
PIVOT (MAX(a) FOR type IN([ALL],[CASH],[DEBT],[EQUITY]))p

答案 3 :(得分:0)

您可以使用UNION ALL首先取消数据的转移,然后使用条件聚合来进行转换:

WITH Cte AS(
    --Your original query here
    SELECT 'ALL' AS type, 0 AS fmsales, 82.1 AS indussales UNION ALL
    SELECT 'CASH' AS type, 0 AS fmsales, 0 AS indussales UNION ALL
    SELECT 'DEBT' AS type, 0 AS fmsales, 62 AS indussales UNION ALL
    SELECT 'EQUITY' AS type, 0 AS fmsales, 20.1 AS indussales
),
CteUnpivot AS(
    SELECT 'fmsales' AS products, 'ALL' AS type, fmsales AS val FROM Cte WHERE type = 'ALL' UNION ALL
    SELECT 'fmsales' AS products, 'CASH' AS type, fmsales AS val FROM Cte WHERE type = 'CASH' UNION ALL
    SELECT 'fmsales' AS products, 'DEBT' AS type, fmsales AS val FROM Cte WHERE type = 'DEBT' UNION ALL
    SELECT 'fmsales' AS products, 'EQUITY' AS type, fmsales AS val FROM Cte WHERE type = 'EQUITY'UNION ALL
    SELECT 'indussales' AS products, 'ALL' AS type, indussales AS val FROM Cte WHERE type = 'ALL' UNION ALL
    SELECT 'indussales' AS products, 'CASH' AS type, indussales AS val FROM Cte WHERE type = 'CASH' UNION ALL
    SELECT 'indussales' AS products, 'DEBT' AS type, indussales AS val FROM Cte WHERE type = 'DEBT' UNION ALL
    SELECT 'indussales' AS products, 'EQUITY' AS type, indussales AS val FROM Cte WHERE type = 'EQUITY'
)
SELECT
    [ALL] = SUM(CASE WHEN type = 'ALL' THEN val ELSE 0 END),
    CASH = SUM(CASE WHEN type = 'CASH' THEN val ELSE 0 END),
    DEBT = SUM(CASE WHEN type = 'DEBT' THEN val ELSE 0 END),
    EQUITY = SUM(CASE WHEN type = 'EQUITY' THEN val ELSE 0 END)
FROM CteUnpivot
GROUP BY products