比如说元数据集是这样的形式id  name date    month
1   A    10元    1月
1   A    20元    1月
1   A    30元    1月
1   A    10元    2月
1   A    20元    2月
1   B    10元    1月
1   B    11元    1月
1   B    20元    2月
1   B    30元    3月
1   B    40元    4月
1   B    50元    5月
要转换成:
id name 1月    2月   3月   4月   5月   6月   7月   8月   9月   10月   11月   12月
1  A    10元  10元
1  A    20元  20元
1  A    30元
1  B    10元  20元  30元  40元  50元
1  B    11元
就是说 一个id的最大行数 取决于 最多记录的那个月的最大行数
然后 没值的月份就显示空白如何实现

解决方案 »

  1.   

    根据month  和  name 产生新的ID,然后根据这个新的ID用case when 判断一下
      

  2.   

    select id,anme,
    max(case when month='1月' then date end) as [1月], 
    max(case when month='2月' then date end) as [2月],
    ...
    max(case when month='12月' then date end) as [12月]
    from (
    select 
    id,name,month,date,(
    select count(*) from tab where id=a.id and name=a.name and month=a.month and date<=a.date) as 序号
    from tab a
    ) as t
    group by id,name,序号
      

  3.   

    参考这个地址:
    http://blog.csdn.net/parss/archive/2008/03/03/2142943.aspx
      

  4.   

    參照:
    行列互转_整理贴3  [推荐]
    http://topic.csdn.net/u/20080614/17/22e73f33-f071-46dc-b9bf-321204b1656f.html