如何根据相关列的值将值从行转到列
How to pivot values from rows to column based on value from related column
我正在尝试根据 3 个值对一组数据进行透视,以便每一行代表与每个 ID 相关的所有配置文件 URL,每一列都与配置文件代码相关
这个问题与我想要实现的非常相似,但是它没有根据另一个值对正在旋转的列进行分组:
所以给出下面的例子table:
Id ProfileCode ProfileURL
-------------------------------------------------------
7ADC7368 IA http://www.domainIA.com/profile1
5C627D6F IA http://www.domainIA.com/profile2
5C627D6F AG http://www.domainAG.com/profile1
5C627D6F AF http://www.domainAF.com/profile1
664B4AE9 IA http://www.domainIA.com/profile3
664B4AE9 AF http://www.domainAF.com/profile2
我希望能改成下面的样子table:
Id IA AG AF
-------------------------------------------------------------------------------------------------------------
7ADC7368 http://www.domainIA.com/profile1 null null
5C627D6F http://www.domainIA.com/profile2 http://www.domainAG.com/profile1 http://www.domainAF.com/profile1
664B4AE9 http://www.domainIA.com/profile3 null http://www.domainAF.com/profile2
这是我一直在尝试使用的代码,但我找不到一种方法将枢轴与配置文件 URL 及其关联的配置文件代码之间的关联相关联。
declare @tmp TABLE (Id NVARCHAR(15), ProfileCode NVARCHAR(2), ProfileURL NVARCHAR(50))
insert into @tmp (Id, ProfileCode, ProfileURL)
values ('7ADC7368', 'IA', 'http://www.domainIA.com/profile1'),
('5C627D6F', 'IA', 'http://www.domainIA.com/profile2'),
('5C627D6F', 'AG', 'http://www.domainAG.com/profile1'),
('5C627D6F', 'AF', 'http://www.domainAF.com/profile1'),
('664B4AE9', 'IA', 'http://www.domainIA.com/profile3'),
('664B4AE9', 'AF', 'http://www.domainAF.com/profile2')
select
pvt.id,
CASE
WHEN ProfileCode = 'IA' THEN ProfileURL
END AS 'IA',
CASE
WHEN ProfileCode = 'AF' THEN ProfileURL
END AS 'AF',
CASE
WHEN ProfileCode = 'AG' THEN ProfileURL
END AS 'AG'
from (
select
Id, ProfileCode, ProfileURL
,ROW_NUMBER() over(partition by ProfileCode order by ProfileURL) as RowNum
from
@tmp
) a
pivot (MAX(ProfileCode) for RowNum in ('IA', 'AF', 'AG') as pvt
对于我正在努力实现的目标,如果有任何帮助或指点,我将不胜感激。
只使用条件聚合:
SELECT id,
MAX(CASE WHEN ProfileCode = 'IA' THEN ProfileURL END) AS IA,
MAX(CASE WHEN ProfileCode = 'AF' THEN ProfileURL END) AS AF,
MAX(CASE WHEN ProfileCode = 'AG' THEN ProfileURL END) AS AG
FROM @tmp t
GROUP BY id;
如果给定 ID 有多个相同的代码,则只需要 ROW_NUMBER()
并且 您希望在不同的行上显示结果。您的示例数据和当前逻辑表明情况并非如此。
我正在尝试根据 3 个值对一组数据进行透视,以便每一行代表与每个 ID 相关的所有配置文件 URL,每一列都与配置文件代码相关
这个问题与我想要实现的非常相似,但是它没有根据另一个值对正在旋转的列进行分组:
所以给出下面的例子table:
Id ProfileCode ProfileURL
-------------------------------------------------------
7ADC7368 IA http://www.domainIA.com/profile1
5C627D6F IA http://www.domainIA.com/profile2
5C627D6F AG http://www.domainAG.com/profile1
5C627D6F AF http://www.domainAF.com/profile1
664B4AE9 IA http://www.domainIA.com/profile3
664B4AE9 AF http://www.domainAF.com/profile2
我希望能改成下面的样子table:
Id IA AG AF
-------------------------------------------------------------------------------------------------------------
7ADC7368 http://www.domainIA.com/profile1 null null
5C627D6F http://www.domainIA.com/profile2 http://www.domainAG.com/profile1 http://www.domainAF.com/profile1
664B4AE9 http://www.domainIA.com/profile3 null http://www.domainAF.com/profile2
这是我一直在尝试使用的代码,但我找不到一种方法将枢轴与配置文件 URL 及其关联的配置文件代码之间的关联相关联。
declare @tmp TABLE (Id NVARCHAR(15), ProfileCode NVARCHAR(2), ProfileURL NVARCHAR(50))
insert into @tmp (Id, ProfileCode, ProfileURL)
values ('7ADC7368', 'IA', 'http://www.domainIA.com/profile1'),
('5C627D6F', 'IA', 'http://www.domainIA.com/profile2'),
('5C627D6F', 'AG', 'http://www.domainAG.com/profile1'),
('5C627D6F', 'AF', 'http://www.domainAF.com/profile1'),
('664B4AE9', 'IA', 'http://www.domainIA.com/profile3'),
('664B4AE9', 'AF', 'http://www.domainAF.com/profile2')
select
pvt.id,
CASE
WHEN ProfileCode = 'IA' THEN ProfileURL
END AS 'IA',
CASE
WHEN ProfileCode = 'AF' THEN ProfileURL
END AS 'AF',
CASE
WHEN ProfileCode = 'AG' THEN ProfileURL
END AS 'AG'
from (
select
Id, ProfileCode, ProfileURL
,ROW_NUMBER() over(partition by ProfileCode order by ProfileURL) as RowNum
from
@tmp
) a
pivot (MAX(ProfileCode) for RowNum in ('IA', 'AF', 'AG') as pvt
对于我正在努力实现的目标,如果有任何帮助或指点,我将不胜感激。
只使用条件聚合:
SELECT id,
MAX(CASE WHEN ProfileCode = 'IA' THEN ProfileURL END) AS IA,
MAX(CASE WHEN ProfileCode = 'AF' THEN ProfileURL END) AS AF,
MAX(CASE WHEN ProfileCode = 'AG' THEN ProfileURL END) AS AG
FROM @tmp t
GROUP BY id;
如果给定 ID 有多个相同的代码,则只需要 ROW_NUMBER()
并且 您希望在不同的行上显示结果。您的示例数据和当前逻辑表明情况并非如此。