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

行转列 数据怎么合并啊?
SQL code

declare @username nvarchar(20),@starttime datetime,@endtime datetime
set @username='用户1';set @starttime='2012/3/4 00:00:00'; set @endtime ='2012/3/10 23:59:00';
select case when datepart(dw,timestr)=1 then contentstr end as sunday ,
                                          case when datepart(dw,timestr)=2 then contentstr end as monday ,
                                          case when datepart(dw,timestr)=3 then contentstr end as tuesday ,
                                          case when datepart(dw,timestr)=4 then contentstr end as Wednesday ,
                                          case when datepart(dw,timestr)=5 then contentstr end as Thursday,
                                          case when datepart(dw,timestr)=6 then contentstr end as Friday,
                                          case when datepart(dw,timestr)=7 then contentstr end as Saturday 
                                    from (select contentstr,timestr from  ERPWorkRiZhi where username=@username and timestr BETWEEN @starttime and @endtime) as a;

这段代码是按时间分组为周日-周六,在没有加MAX时,它的结果是
sunday monday tuesday Wednesday Thursday Friday Saturday
内容 NULL NULL NULL NULL NULL NULL
NULL 内容 NULL NULL NULL NULL NULL  
NULL 内容 NULL NULL NULL NULL NULL
NULL NULL 内容 NULL NULL NULL NULL
因为是文本的,没法用MAX来聚合它,我希望得到的结果是
内容 内容 内容 NULL NULL NULL NULL
NULL 内容 NULL NULL NULL NULL NULL  
求高手指点下,谢谢。
如果你解决了还有个贴子也是一样的问题,帮我回答下,我好结贴。http://topic.csdn.net/u/20120308/08/7f3fef1a-efe1-42bd-96d6-daf97425b2a6.html

------解决方案--------------------
文本可用MAX聚合.
------解决方案--------------------
把表结构拿出来大家看看。