[英]Transposing rows from one table based on column values in another table
I asked a similar question [here]: Transposing groups of rows with the same ID into another table but I don't want to have to write out each line for each attribute name. 我在[此处]提出了类似的问题: 将具有相同ID的行组转置到另一个表中,但我不想为每个属性名称写出每一行。 I want to be able to take rows in one column from TABLE1 (say there's 50 unique rows) and transpose them efficiently into TABLE2.
我希望能够从TABLE1的一列中获取行(比如说有50个唯一的行),并将它们有效地转置到TABLE2中。
This is a small example of a table of ATTR_NMEs (attribute names) in one column: 这是一列ATTR_NME(属性名称)表的小示例:
ID ATTR_NME ATTR_VAL
1000 UPC 00015582981001
1000 Price 15.99
1000 Brand Oreo
1005 UPC 00038762291010
1005 Price 12.50
1005 Brand Sargento
1010 UPC 00198872499000
1010 Price 4.99
1010 Brand Olay
So ID, UPC, Price, and Brand should be their own columns with the ATTR_VAL as the rows of data. 因此,ID,UPC,价格和品牌应该是它们自己的列,并将ATTR_VAL作为数据行。 HERE'S THE DIFFERENCE BETWEEN THIS QUESTION AND MY PREVIOUS ONE: say I have a table with 50 different ATTR_NMEs, I want to use that column of data with all those attribute names to create the new tranposed table without having to write the following 50 times for the different attributes:
这是我的问题与上一个问题的区别:假设我有一个包含50个不同ATTR_NME的表,我想使用具有所有这些属性名称的那列数据来创建新的转置表,而不必为该表写以下50次不同的属性:
MAX(CASE WHEN ATTR_NME = 'UPC' THEN ATTR_VAL END) AS UPC
I tried to declare a variable for the rows in the ATTR_NME column like this: 我试图为ATTR_NME列中的行声明一个变量,如下所示:
DECLARE @itm varchar(100);
SET @itm = (
SELECT ATTR_NME
FROM TABLE1
);
SELECT ID,
MAX(CASE WHEN ATTR_NME = @itm THEN ATTR_VAL END) AS [@itm]
FROM TABLE1
GROUP BY ID;
But that didn't work for multiple reasons; 但是,由于多种原因,这种方法行不通。 one being it wanted me to wrap an aggregate function around ATTR_NME where I SET @itm (but then I only got one attribute name).
有人希望我在我设置@itm的ATTR_NME周围包装一个聚合函数(但后来我只有一个属性名)。 It also didn't label/name the column as I wanted it to.
它也没有按我想要的方式标记/命名列。 Is it even possible to do what I want to do?
甚至有可能做我想做的事吗? If so, how?
如果是这样,怎么办? TIA.
TIA。
Are you talking about Pivot
? 您是在谈论
Pivot
吗?
If you need to pivot
this table, you cannot avoid putting the ATTR_NME
50 times in the below query (see comment) 如果需要
pivot
此表,则无法避免在下面的查询中放置ATTR_NME
50次(请参阅注释)
WITH A
AS
(
SELECT '1000' as ID, 'UPC ' as ATTR_NME, '00015582981001' as ATTR_VAL
UNION ALL SELECT '1000' as ID, 'Price' as ATTR_NME, '15.99 ' as ATTR_VAL
UNION ALL SELECT '1000' as ID, 'Brand' as ATTR_NME, 'Oreo ' as ATTR_VAL
UNION ALL SELECT '1005' as ID, 'UPC ' as ATTR_NME, '00038762291010' as ATTR_VAL
UNION ALL SELECT '1005' as ID, 'Price' as ATTR_NME, '12.50 ' as ATTR_VAL
UNION ALL SELECT '1005' as ID, 'Brand' as ATTR_NME, 'Sargento ' as ATTR_VAL
UNION ALL SELECT '1010' as ID, 'UPC ' as ATTR_NME, '00198872499000' as ATTR_VAL
UNION ALL SELECT '1010' as ID, 'Price' as ATTR_NME, '4.99 ' as ATTR_VAL
UNION ALL SELECT '1010' as ID, 'Brand' as ATTR_NME, 'Olay ' as ATTR_VAL
)
SELECT *
FROM A
PIVOT
(MAX(ATTR_VAL) FOR ATTR_NME in ([Price],[Brand],[UPC]) ) -- here you need to put all the 50 ATTR_NAME
as PIVOTTABLE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.