我现在正在尝试整理公司以前的销售数据,如下所示:
Item1 Item2
Clock Spring
Hands Nuts
Dial Clock
Clock Hands
Hands Spring
我需要的是这个
Item1 Product1 Product2
Clock Spring Hands
Hands Nuts Spring
Dial Clock
我大约有1000个数据点,所以我想要一些动态的东西,但是我走了这么远:
SELECT transdata.item1, Product1, Product2, Product3
(
SELECT transdata.item1, transdata.item2,
'Product' + CAST(ROW_NUMBER() over (Partition By transdata.[item1] Order By transdata.item1) AS Varchar(10)) AS ColSequence
FROM transdata
) Temp
PIVOT
(
MAX(Product)
For ColSequence in (Product1, Product2, Product3)
)PIV
它说我有语法错误
答案 0 :(得分:1)
这在MS Access中确实很痛苦。但是此相对简单的版本最多可用于两列:
select item1, min(item2) as product1,
iif(min(item2) <> max(item2), max(item2), null) as product2
from transdata
group by item1