日期:2014-05-18  浏览次数:20434 次

求如下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 四月,
ISNU