尝试将多个列放入行中。预期结果是
这是我试过的样本数据。我对unpivot也持开放态度,如果总体上更快的话。完整数据有15个attributeid、attributedata列。
DROP TABLE Attribute;
CREATE TABLE Attribute
(
Producttitle varchar(200),
AttributeID_1 varchar(50),
AttributeData_1 varchar(50),
AttributeID_2 varchar(50),
AttributeData_2 varchar(50),
AttributeID_3 varchar(50),
AttributeData_3 varchar(50)
);
INSERT INTO Attribute
VALUES ('title1', '3145', 'Specific', '30', 'Yes', '40', 'Pink')
INSERT INTO Attribute
VALUES ('title2', '17', 'Stainless', '19', 'smoke', '19', 'Something');
SELECT
Producttitle,
[AttributeID],
[AttributeData]
FROM
Attribute
CROSS APPLY
(SELECT 'Indicator1', [AttributeID_1] UNION ALL
SELECT 'Indicator2', [AttributeID_2] UNION ALL
SELECT 'Indicator3', [AttributeID_3]) c (indicatorname, [AttributeID])
CROSS APPLY
(SELECT 'Indicator1', [AttributeData_1] UNION ALL
SELECT 'Indicator2', [AttributeData_2] UNION ALL
SELECT 'Indicator3', [AttributeData_3]) d (indicatorname, [AttributeData]);
2条答案
按热度按时间ee7vknir1#
你可以用
cross apply
取消激活数据集。用它更简单values()
:请注意,这是因为这两组列具有一致的数据类型,否则需要进行额外的转换。
axzmvihb2#
gmb的解决方案确实很酷,但基本工会也会奏效: