有关Sql中自连接查询问题

来源:百度知道 编辑:UC知道 时间:2024/07/05 15:25:34
问题:
表TestColor
title color sign
a 红 111
b 红 222
a 绿 333
c 红 444
转视图:
title 红 绿
a 111 333
b 222 null
c 444 null

求一个动态的查询语句。即颜色为值不只为红或蓝,当为自己想要的颜色时都可以判断?

呵呵 试一下这个语句 对了给我加加分呀
declare @sql varchar(100)
set @sql = 'select title '
select @sql = @sql + ' , max(case color when ''' + color + ''' then sign else 0 end) [' + color + ']'
from (select distinct color from tb) as a
set @sql = @sql + ' from tb group by title'
exec(@sql)