表table:
   date1       name     type
  20100101     王二     迟到
  20100102     张三     迟到
  20100102     王二     迟到
  20100103     张三     早退
  20100103     王二     早退想要得到2010年01月的结果:
   name     1日    2日    3日    4日     5日  。31日
   王二    迟到   迟到   早退   
   张三           迟到   早退请问在数据库里怎么实现他,SQL语句?存储过程?函数?怎么实现?实现代码?

解决方案 »

  1.   

    如果没有,可以
    select name,
    max(decode(date1,'20100101',type,null) '1日',
    max(decode(date1,'20100102',type,null) '2日',
    ...
    max(decode(date1,'20100131',type,null) '31日'
    from table group by name;
    这样看上去比较笨,也可以写一个存储过程来拼接
      

  2.   

    固定要31天的话,可以像楼上这么写,列别名的单引号要改成双引号
    如果不固定的话只能通过plsql,参考
    http://topic.csdn.net/u/20100109/13/6a10c168-f190-4766-b838-adbf03c4ac7b.html?64992