表一包含
ID|Name
1 Mary
2 John
表二包含
ID|Color
1 Red
2 Blue
2 Green
2 Black
我想结束的是
ID|Name|Red|Blue|Green|Black
1 Mary Y Y
2 John Y Y Y
谢谢你的帮助。
感谢您的回复。我将重新发布此内容,并提供一些有关我正在尝试执行的操作的附加信息,这可能会使问题变得复杂。有人可以关闭这个吗?
如果您使用 T-SQL,则可以使用 PIVOT (http://msdn.microsoft.com/en-us/library/ms177410.aspx http://msdn.microsoft.com/en-us/library/ms177410.aspx)
这是我使用的查询:
declare @tbl_names table(id int, name varchar(100))
declare @tbl_colors table(id int, color varchar(100))
insert into @tbl_names
select 1, 'Mary'
union
select 2, 'John'
insert into @tbl_colors
select 1, 'Red'
union
select 1, 'Blue'
union
select 2, 'Green'
union
select 2, 'Blue'
union
select 2, 'Black'
select name,
case when [Red] is not null then 'Y' else '' end as Red,
case when [Blue] is not null then 'Y' else '' end as Blue,
case when [Green] is not null then 'Y' else '' end as Green,
case when [Black] is not null then 'Y' else '' end as Black
from
(
select n.id, name, color from @tbl_names n
inner join @tbl_colors c on n.id = c.id
) as subq
pivot
(
min(id)
FOR color IN ([Red], [Blue], [Green], [Black])
) as pvt
这是输出:
John Y Y Y
Mary Y Y
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)