Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 参数化数据透视表_Sql Server_Pivot - Fatal编程技术网

Sql server 参数化数据透视表

Sql server 参数化数据透视表,sql-server,pivot,Sql Server,Pivot,我们有三张桌子 tid_颜色-参数化表 -------------------------- ID ColorDescription -------------------------- 1 Green 2 Yellow 3 Red ------------------------- -------------------------- ID CARDescription ------------------

我们有三张桌子

tid_颜色-参数化表

--------------------------
ID         ColorDescription
--------------------------
1          Green
2          Yellow
3          Red
-------------------------
--------------------------
ID         CARDescription
-------------------------
1          Car X
2          Car Y
3          Car Z
-------------------------- 
tid_车辆-参数化表

--------------------------
ID         ColorDescription
--------------------------
1          Green
2          Yellow
3          Red
-------------------------
--------------------------
ID         CARDescription
-------------------------
1          Car X
2          Car Y
3          Car Z
-------------------------- 
表(车主)(汽车)

------------------------------------------------
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_汽车的外键

ColorId是tid_颜色的外键

如果我们编码:

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
但我想在标题中的颜色,所以我有代码作为

SELECT CarId, [1] as 'Green', [2] as 'Yellow', [3] as 'Red', [1]+[2]+[3] as 'total' 
FROM
(SELECT CarID, colorId
 FROM table_owners_cars tocar
 LEFT JOIN tid_car tc  ON tocar.CarId=tc.Id) p 
PIVOT
( 
 COUNT (ColorId)
 FOR ColorId IN ( [1], [2], [3]) 
) AS pvt
使用以下SQL生成的表:

  ---------------------------------------------
   Id        Car     Green Yellow  Red    Total
   ---------------------------------------------
    1         1        3     1     1       5
    2         2        0     1     0       1                
    3         3        1     0     0       1                   
   ---------------------------------------------
它无法将汽车的描述(CarX、CarY、CarZ)放在汽车列中。。。而不是前面代码中的第一个选择,我尝试将其作为

选择tc.CardDescription,[1]为“绿色”;[2]为“黄色”;[3]为“红色”;[1]+[2]+[3]为“总计”

然后它抛出

无法绑定多部分标识符“tc.CardDescription”

我想要卡片说明,而不是上表中显示的ID。我希望得到的表格如下

我想准确地按如下方式旋转:

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

如何做到这一点?谢谢

您可以连接到数据透视结果:

 SELECT pvt.CarID, tc.Description AS Car, [1] as 'Green', [2] as 'Yellow', [3] as 'Red', [1]+[2]+[3] as 'total' 
    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