我在查看此示例时尝试使用 POVIT这 是代码:
CREATE DATABASE DEMO
GO
USE DEMO
GO
-- Creating table for demo
IF (object_id('TblPivot','U') > 0)
DROP TABLE TblPivot
CREATE TABLE TblPivot
(
ItemCode int,
ItemName varchar(100),
ItemColour varchar(50)
)
GO
-- Inerting some sample records
INSERT INTO TblPivot
SELECT 1,'Samsung Mobile','Red'
UNION ALL
SELECT 2,'Nokia Mobile','Blue'
UNION ALL
SELECT 3,'Nokia Mobile','Green'
UNION ALL
SELECT 4,'Motorola Mobile','Red'
UNION ALL
SELECT 5,'Samsung Mobile','Green'
UNION ALL
SELECT 2,'Nokia Mobile','Blue'
UNION ALL
SELECT 1,'Samsung Mobile','Red'
UNION ALL
SELECT 2,'Nokia Mobile','Blue'
GO
这是 PIVOT 选择
-- Getting table data
SELECT
ItemCode,
ItemName,
ItemColour
from TblPivot
GO
-- Getting agreegated data using Pivot and converted rows to column
SELECT
*
FROM
(
SELECT
ItemCode,
ItemName,
ItemColour
FROM TblPivot
) AS P
PIVOT
(
Count(ItemName) FOR ItemColour IN (Red, Blue, Green)// Here is the Issue
where it knows what words to give it like Red,Blue,Green what I want is to use
what ever the ItemColur it could be 100s What ever you get
from the data base use that for the `IN(ItemColur)`
) AS pv
GO
它一直说 PIVOT 运算符中指定的列名“ItemColur”与 PIVOT 参数中的现有列名冲突。我怎么能得到这个工作,我什至试图做一个临时表没有工作