交叉 合并

aplesen 2010-05-10 02:06:53
上次问过的,不好意思啊,这是我的数据表,其中各字段意思为,日期,分店代码,员工姓名,班别

我要实现下面这样的效果

有点问题,就是 大家看到的那个 双飞燕 不是 801 这个机构的 其它都是,我的代码是这样写的 请问我该怎么修改,谢谢

declare @sql varchar(max),@i int,@name char(100),@Outal char(4) declare @tb table (id int identity(1,1),_name char(100)) select @Outal='801' insert into @tb select distinct cName from View_Scheduling where sOutal=@Outal select @i=1 while(@i<=(select count(*) from @tb)) begin select top 1 @name=_name from @tb where id not in (select top (@i-1) id from @tb) set @sql=isnull(@sql+','+@name+'=case cName when '''+@name+''' then temp end',@name+'=case cName when '''+@name+''' then temp end') set @i=@i+1 end set @sql='select sDate,'+@sql+' from (select sDate,cName,temp=stuff((select '',''+rtrim(eName) from View_Scheduling where sDate=t.sDate for xml path('''')),1,1,'''') from View_Scheduling t group by sDate,cName )t'exec(@sql)
...全文
58 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
永生天地 2010-05-10
  • 打赏
  • 举报
回复
上面改成 from View_Scheduling t where sOutal=''801'' group by sDate,cName )t'
加上条件 where sOutal=''801''
永生天地 2010-05-10
  • 打赏
  • 举报
回复
SET @sql = 'select sDate,' + @sql +  
' from (select sDate,cName,temp=stuff((select '','
'+rtrim(eName) from View_Scheduling where sDate=t.sDate for xml path('''
')),1,1,'''') from View_Scheduling t where sOutal=''801''group by sDate,cName )t'


[Quote=引用 4 楼 xys_777 的回复:]
SET @sql = 'select sDate,' + @sql +
' from (select sDate,cName,temp=stuff((select '','
'+rtrim(eName) from View_Scheduling where sDate=t.sDate for xml path('''
')),1,1,'''') from View_Schedu……
[/Quote]
永生天地 2010-05-10
  • 打赏
  • 举报
回复
SET @sql = 'select sDate,' + @sql +
' from (select sDate,cName,temp=stuff((select '','
'+rtrim(eName) from View_Scheduling where sDate=t.sDate for xml path('''
')),1,1,'''') from View_Scheduling t where sOutal<>''801''group by sDate,cName )t'
aplesen 2010-05-10
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20100423/17/914d517d-a19e-4c75-bbf6-950188d80d58.html
上次发的帖子
aplesen 2010-05-10
  • 打赏
  • 举报
回复
图1
http://hi.csdn.net/space-4282885-do-album-picid-540028-goto-down.html

图2
http://hi.csdn.net/space-4282885-do-album-picid-540027-goto-down.html
aplesen 2010-05-10
  • 打赏
  • 举报
回复
怎么还看不到图片啊,图片在我相册里 怎么把显示出来啊 救命啊

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧