有关Sql中自连接查询问题

发布时间:2024-05-17 19:39 发布:上海旅游网

问题描述:

问题:
表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)

热点新闻