读书人

求如下SQL 先多谢了

发布时间: 2012-01-24 23:11:54 作者: rapoo

求如下SQL 先谢谢了
姓名 月份 日期
a 一月 1-1
a 二月 2-2
a 三月 3-3
a 四月 4-4
a 五月 5-5
a 六月 6-6
a 七月 7-7
a 八月 8-8
a 九月 9-9
a 十月 10-1
a 十一月 11-1
a 十二月 12-1
b 一月 1-1
b 二月 2-2

得出如下结果
-----------------------------------------------
姓名 一月 二月三月四月五月六月七月八月九月十月十一月十二月
a 1-1 2-2 3-3 4-4 5-5 6-6 7-7 8-8 9-9 10-1 11-1 12-1
b 1-1 2-2

[解决办法]
select
姓名,
max(case 月份 when '一月 ' then 日期 end) as 一月,
max(case 月份 when '二月 ' then 日期 end) as 二月,
max(case 月份 when '三月 ' then 日期 end) as 三月,
max(case 月份 when '四月 ' then 日期 end) as 四月,
max(case 月份 when '五月 ' then 日期 end) as 五月,
max(case 月份 when '六月 ' then 日期 end) as 六月,
max(case 月份 when '七月 ' then 日期 end) as 七月,
max(case 月份 when '八月 ' then 日期 end) as 八月,
max(case 月份 when '九月 ' then 日期 end) as 九月,
max(case 月份 when '十月 ' then 日期 end) as 十月,
max(case 月份 when '十一份 ' then 日期 end) as 十一月,
max(case 月份 when '十二份 ' then 日期 end) as 十二月
from
表名
group by
姓名
[解决办法]
月份固定的时候:
select
姓名,
ISNULL(max(case 月份 when '一月 ' then 日期 end), ' ') as 一月,
ISNULL(max(case 月份 when '二月 ' then 日期 end), ' ') as 二月,
ISNULL(max(case 月份 when '三月 ' then 日期 end), ' ') as 三月,
ISNULL(max(case 月份 when '四月 ' then 日期 end), ' ') as 四月,
ISNULL(max(case 月份 when '五月 ' then 日期 end), ' ') as 五月,


ISNULL(max(case 月份 when '六月 ' then 日期 end), ' ') as 六月,
ISNULL(max(case 月份 when '七月 ' then 日期 end), ' ') as 七月,
ISNULL(max(case 月份 when '八月 ' then 日期 end), ' ') as 八月,
ISNULL(max(case 月份 when '九月 ' then 日期 end), ' ') as 九月,
ISNULL(max(case 月份 when '十月 ' then 日期 end), ' ') as 十月,
ISNULL(max(case 月份 when '十一份 ' then 日期 end), ' ') as 十一月,
ISNULL(max(case 月份 when '十二份 ' then 日期 end), ' ') as 十二月
from
表名
group by
姓名


------------------------------
月份不固定的时候:
DECLARE @SQL VARCHAR(4000)
SET @SQL = 'SELECT [姓名] '
SELECT DISTINCT @SQL = @SQL + '
,MAX(CASE 月份 WHEN ' ' ' + 月份 + ' ' ' THEN ISNULL(日期, ' ' ' ') END) AS [ ' + 月份+ '] '
from
(SELECT DISTINCT 月份 FROM 表名 ) a
ORDER BY
月份

SET @SQL = @SQL + 'FROM 表名 GROUP BY 姓名 '

exec (@SQL)


[解决办法]
=============
create table t([name] nvarchar(10), [month] nvarchar(20), 日期 varchar(10))


insert into t
select 'a ', '一月 ', '1-1 '
union all
select 'a ', '二月 ', '2-2 '
union all
select 'a ', '三月 ', '3-3 '
union all
select 'a ', '四月 ', '4-4 '
union all
select 'a ', '五月 ', '5-5 '
union all
select

'a ', '六月 ', '6-6 '
union all
select
'a ', '七月 ', '7-7 '
union all
select
'a ', '八月 ', '8-8 '
union all
select 'a ', '九月 ', '9-9 '
union all
select 'a ', '十月 ', '10-1 '
union all
select 'a ', '十一月 ', '11-1 '
union all
select 'a ', '十二月 ', '12-1 '
union all
select 'b ', '一月 ', '1-1 '
union all
select 'b ', '二月 ', '2-2 '

===============SQL
declare @sql nvarchar(4000)
select @sql = 'select [name] as ' + '姓名 '
select @sql = @sql + ',min(case [month] when ' ' ' + [month] + ' ' ' then 日期 end) [ ' + [month] + '] '
from (select distinct [month] from t ) as AAA
select @sql = @sql + ' from t group by [name] '

exec(@sql)

============Result
姓名 一月 九月 五月 三月 四月 七月 十一月 十月 十二月 二月 八月 六月
a 1-1 9-9 5-5 3-3 4-4 7-7 11-1 10-1 12-1 2-2 8-86-6
b 1-1 2-2

读书人网 >SQL Server

热点推荐