数据透视示例中的总列数

时间:2018-03-16 16:38:18

标签: sql sql-server pivot

如果需要,请在此处查看背景信息: Pivoting a table with parametrization

我们有3张桌子。

tid_color - 参数化表

--------------------------
ID         ColorDescription
--------------------------
1          Green
2          Yellow
3          Red
-------------------------

tid_car - 参数化表

--------------------------
ID         CARDescription
-------------------------
1          Car X
2          Car Y
3          Car Z
-------------------------- 

table_owners_cars

------------------------------------------------
ID         CarID       ColorID      Owner
------------------------------------------------
1           1             1           John
2           1             2           Mary
3           1             3           Mary  
4           1             3           Giovanni     
5           2             2           Mary
6           3             1           Carl
7           1             1           Hawking
8           1             1           Fanny 
------------------------------------------------

CarID是tid_car的外键

ColorId是tid_color的外键

如果我们编码:

SELECT tcar.CarDescription, tco.ColorDescription, Count(*) as Total
FROM table_owners_cars tocar
LEFT JOIN tid_color tco ON tco.Id = tocar.ColorId
LEFT JOIN tid_Car tcar ON tcar.Id = tocar.CarId
GROUP BY CarDescription, ColorDescription 

结果为:

Id CarDescription     ColorDescription  Total
1       CarX               Green          3
2       CarX               Yellow         1
3       CarX               Red            1
4       CarY               Yellow         1
5       CarZ               Green          1

我想完全按照以下方式进行转移

   ---------------------------------------------
   Id        Car     Green Yellow  Red    Total
   ---------------------------------------------
    1       CarX        3     1     1       5
    2       CarY        0     1     0       1                
    3       CarZ        1     0     0       1                   
   ---------------------------------------------

现在:  我们想要计算table_owners_cars特定列中每一行的总数,这个值接近总数,就像我们在最后一列(括号之间)中看到的那样。对于colorID,CarX WITH NULL(与其他Car一样可以发生),我们想知道carX,carY,CarZ的所有数量(有和没有(= null或0)分配ColorId

   ---------------------------------------------------
   Id        Car     Green Yellow  Red  Violet   Total
   ---------------------------------------------------
    1       CarX        3     1     1     0     5 (40)
    2       CarY        0     1     0     0     1 (35)               
    3       CarZ        1     0     0     0     1 (4)                   
   ---------------------------------------------------
   DESIRED TABLE

尝试使用代码(非常类似于上述超链接中提供的代码):

SELECT pvt.CarID, tc.Description AS Car, CONCAT (' [1] as 'Green', [2] as 'Yellow', [3] as 'Red', [1]+[2]+[3] as 'total'', '(', count(*), ')' )
        FROM
        (SELECT CarID, colorId
         FROM table_owners_cars tocar
         ) p 
        PIVOT
        ( 
         COUNT (ColorId)
         FOR ColorId IN ( [1], [2], [3]) 
        ) AS pvt
        INNER JOIN tid_car tc  ON pvt.CarId=tc.Id
        group by p.Car

这不起作用。单引号也是concat的噩梦。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我发现这些查询更容易使用条件聚合:

SELECT CarId, Description,
       SUM(CASE WHEN color = 'Green' THEN 1 ELSE 0 END) as Green,
       SUM(CASE WHEN color = 'Yellow' THEN 1 ELSE 0 END) as Yellow,
       SUM(CASE WHEN color = 'Red' THEN 1 ELSE 0 END) as Red,
       SUM(CASE WHEN color IN ('Green', 'Yellow', 'Red') THEN 1 ELSE 0 END) as total_gyr,
       COUNT(*) as total
FROM table_owners_cars tocar
GROUP BY CarId, Description;

我认为没有理由将两个总计合并为一个字符串列 - 而不是将它们放在单独的整数列中。但是,如果您愿意,可以将它们组合起来。