2017-12-21 28 views
3

나는 다음과 같은 코드가 있습니다PIVOT의 SQL - 안내

SELECT * 
    FROM 
     ( 
     SELECT p.ProductID, pc.Name, ISNULL(p.Color, 'Uncolored') AS Color 
     FROM SalesLT.ProductCategory AS pc 
     INNER JOIN SalesLT.Product AS p ON pc.ProductCategoryID = p.ProductCategoryID 
     ) 
    AS PPC 
    PIVOT (COUNT(ProductID) FOR COLOR IN ([Red], [Blue], [Black], [Silver], [Yellow], [Grey], [Multi], [Uncolored])) 
    AS ColorPivotTable 

이것은 follwing을 출력을 제공을 :

Results

나는이

에 총 열을 적용하는 가장 좋은 방법을 알고 싶습니다

원하는 출력 Desired output

피드백에 대해 미리 감사드립니다.

+1

는 뜻은 "총 열 적용"을 어떻게 설명한다. –

+0

안녕하세요. 색상별로 모든 제품을 요약 한 총 열 ('색상이없는 열'오른쪽), 즉 총 32 개의 산악 자전거입니다. 또한 토 칼럼은 색상별로 모든 제품을 요약 한 밑바탕입니다. –

+0

@ GordonLinoff 롤업이 OP를 만족시킬지도 모른다고 생각했지만이 방법은 잘 모릅니다. –

답변

0

당신은 여기에 비슷한 질문 찾을 수 : CUBE를 사용

Using pivot table with column and row totals in sql server 2008

이 솔루션은 CTE를 사용

WITH SalesLT_ProductCategory as (
    SELECT * FROM (
     VALUES 
      (1, 'Mountain Bikes'), 
      (2, 'Road Bikes'), 
      (3, 'Touring Bikes'), 
      (4, 'Brakes') 
      -- etc... 
     ) AS a (ProductCategoryID, Name) 
), SalesLT_Product AS (
    SELECT * FROM (
     VALUES 
      (1, 1, 'Red'), 
      (1, 1, 'Blue'), 
      (1, 1, 'Blue'), 
      (1, 1, 'Blue'), 

      (1, 2, 'Red'), 
      (1, 2, 'Red'), 
      (1, 2, 'Blue'), 
      (1, 2, 'Black'), 

      (1, 3, 'Black'), 
      (1, 3, 'Yellow'), 
      (1, 3, 'Grey'), 
      (1, 3, 'Grey'), 
      (1, 3, 'Grey'), 

      (1, 4, 'Red'), 
      (1, 4, 'Multi'), 
      (1, 4, 'Multi'), 
      (1, 4, 'Uncolored'), 
      (1, 4, 'Uncolored'), 
      (1, 4, 'Uncolored') 
      -- etc... 
     ) AS a (ProductID, ProductCategoryID, Color) 
), BaseData AS (
     SELECT p.ProductID, pc.Name, ISNULL(p.Color, 'Uncolored') AS Color 
     FROM SalesLT_ProductCategory AS pc 
     INNER JOIN SalesLT_Product AS p ON pc.ProductCategoryID = p.ProductCategoryID 
) 
SELECT 
    Name, 
    COALESCE(Red, 0) AS Red, 
    COALESCE(Blue, 0) AS Blue, 
    COALESCE(Black, 0) AS Black, 
    COALESCE(Silver, 0) AS Silver, 
    COALESCE(Yellow, 0) AS Yellow, 
    COALESCE(Grey, 0) AS Grey, 
    COALESCE(Multi, 0) AS Multi, 
    COALESCE(Uncoloured, 0) AS Uncoloured, 
    Total 
FROM (
    SELECT 
    COALESCE(Name, 'Total') AS Name, 
    COALESCE(Color, 'Total') AS Color, 
    COUNT(*) AS Count 
    FROM BaseData 
    GROUP BY CUBE (Name, Color) 
) AS t 
PIVOT (
    SUM(Count) FOR Color IN (
    Red, Blue, Black, Silver, Yellow, 
    Grey, Multi, Uncoloured, Total 
) 
) AS p 
ORDER BY CASE Name WHEN 'Total' THEN 1 ELSE 0 END, Name 

이 될 수를, 대체 솔루션이 될 수

WITH SalesLT_ProductCategory as (
    SELECT * FROM (
     VALUES 
      (1, 'Mountain Bikes'), 
      (2, 'Road Bikes'), 
      (3, 'Touring Bikes'), 
      (4, 'Brakes') 
      -- etc... 
     ) AS a (ProductCategoryID, Name) 
), SalesLT_Product as (
    SELECT * FROM (
     VALUES 
      (1, 1, 'Red'), 
      (1, 1, 'Blue'), 
      (1, 1, 'Blue'), 
      (1, 1, 'Blue'), 

      (1, 2, 'Red'), 
      (1, 2, 'Red'), 
      (1, 2, 'Blue'), 
      (1, 2, 'Black'), 

      (1, 3, 'Black'), 
      (1, 3, 'Yellow'), 
      (1, 3, 'Grey'), 
      (1, 3, 'Grey'), 
      (1, 3, 'Grey'), 

      (1, 4, 'Red'), 
      (1, 4, 'Multi'), 
      (1, 4, 'Multi'), 
      (1, 4, 'Uncolored'), 
      (1, 4, 'Uncolored'), 
      (1, 4, 'Uncolored') 
      -- etc... 
     ) AS a (ProductID, ProductCategoryID, Color) 
), PivotData AS (
    -- your query 
    SELECT * 
    FROM 
     ( 
     SELECT p.ProductID, pc.Name, ISNULL(p.Color, 'Uncolored') AS Color 
     FROM SalesLT_ProductCategory AS pc 
     INNER JOIN SalesLT_Product AS p ON pc.ProductCategoryID = p.ProductCategoryID 
     ) 
    AS PPC 
    PIVOT (COUNT(ProductID) FOR COLOR IN ([Red], [Blue], [Black], [Silver], [Yellow], [Grey], [Multi], [Uncolored])) 
    AS ColorPivotTable 
), ColumnTotals AS (
    -- column totals 
    SELECT 
      'Total' AS Name 
     , SUM(Red) AS Red 
     , SUM(Blue) AS Blue 
     , SUM(Black) AS Black 
     , SUM(Silver) AS Silver 
     , SUM(Yellow) AS Yellow 
     , SUM(Grey) AS Grey 
     , SUM(Multi) AS Multi 
     , SUM(Uncolored)AS Uncolored 
    FROM PivotData 
), PivotDataWithRowTotals AS (
SELECT * FROM PivotData 
UNION ALL 
SELECT * FROM ColumnTotals 
) 
SELECT P.* 
    -- row totals 
    , P.Red + P.Blue + P.Black + P.Silver + P.Yellow + P.Grey + P.Multi + P.Uncolored as Total 
FROM PivotDataWithRowTotals AS P 
+1

유즈 케이스에 따라'CUBE' 또는'ROLLUP' /'GROUPING SETS'을 사용하여 수동으로'UNION ALL' 연결을 단순화 할 수 있습니다. 이 블로그 게시물에 나와있는 것처럼 더 나은 계획을 세울 수 있습니다 (https://blog.jooq.org/2017/12/22/creating-a-microsoft-excel-style-pivot- table -with-grand-totals-in-sql /) –

+0

제안에 감사드립니다. 게시물에서와 같이 CUBO/ROLLUP을 사용하는 대안 솔루션을 게시하려고 시도합니다 https://stackoverflow.com/questions/17140559/using-pivot- table-with-column-and-row-total-in-sql-server-2008] – Ezin82

+0

걱정할 필요가 없습니다. 나는 이미 그것을했다 ;-) –

4

이것은 CUBE() (or GROUPING SETS) calculation의 좋은 예이며, d와 PIVOT representation, I had to write a blog post about it.

여기 당신이 찾고있는 정확하게 생산하고 그 해결책 :

WITH Bikes(Name, Colour) AS (
    SELECT * FROM (
    VALUES ('Mountain Bikes', 'Black'), 
      ('Mountain Bikes', 'Black'), 
      ('Mountain Bikes', 'Silver'), 
      ('Road Bikes', 'Red'), 
      ('Road Bikes', 'Red'), 
      ('Road Bikes', 'Red'), 
      ('Road Bikes', 'Black'), 
      ('Road Bikes', 'Yellow') 
) AS Bikes(Name, Colour) 
) 
SELECT 
    Name, 
    COALESCE(Red, 0) AS Red, 
    COALESCE(Blue, 0) AS Blue, 
    COALESCE(Black, 0) AS Black, 
    COALESCE(Silver, 0) AS Silver, 
    COALESCE(Yellow, 0) AS Yellow, 
    COALESCE(Grey, 0) AS Grey, 
    COALESCE(Multi, 0) AS Multi, 
    COALESCE(Uncoloured, 0) AS Uncoloured, 
    Total 
FROM (
    SELECT 
    Coalesce(Name, 'Total') Name, 
    COALESCE(Colour, 'Total') Colour, 
    COUNT(*) Count 
    FROM Bikes 
    GROUP BY CUBE (Name, Colour) 
) AS t 
PIVOT (
    MAX(Count) FOR Colour IN (
    Red, Blue, Black, Silver, Yellow, Grey, Multi, Uncoloured, Total 
) 
) AS p 
ORDER BY CASE Name WHEN 'Total' THEN 1 ELSE 0 END, Name 

SQLFiddle here