求一句SQL,各路神仙帮忙啊!

jinghuanzhang 2008-07-18 03:53:55
各位大仙,小神有事相求:
现有A 表一张,EmpCode,Quantity,ItemModel
张三 300 A
李四 150 A
赵五 200 B
*** 120 C
*** *** **

其中:itemmodel 为 N 种
现在想 得到 显示 如下:
A B C D ********
张三 300 0 0 0 *****
李四 150 0 0 0 *****
*** *** **


PS:各位大仙,快亮法宝吧!!!!!!!!本人为新人,不知道怎么给分(也不知道有分给!)
...全文
67 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhiguo2008 2008-07-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jinghuanzhang 的回复:]
各位大仙,就不能用 语句吗?,非要搞得跟 存储过程 似的,小的 看得不是很懂!!!!不过确认 能够 实现的!!!先拜谢
[/Quote]
....
jinghuanzhang 2008-07-18
  • 打赏
  • 举报
回复
各位大仙,就不能用 语句吗?,非要搞得跟 存储过程 似的,小的 看得不是很懂!!!!不过确认 能够 实现的!!!先拜谢
tanweibiao2000 2008-07-18
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
select @sql=' select EmpCode '
select @sql=@sql+','+aa.ItemModel+'=sum(case ItemModel when '''+aa.ItemModel+''' then quantity else 0 end ) '
from (select distinct ItemModel From A) as aa
select @sql=@sql+' From A group by EmpCode '
exec( @sql)
BernardSun 2008-07-18
  • 打赏
  • 举报
回复
那就用动态的,其实我也现学现卖的,呵呵
jinghuanzhang 2008-07-18
  • 打赏
  • 举报
回复
其中:itemmodel 为 N 种


不好意思,不知是我不会灵活应用还是,LS 大哥没看明白,我现在 ITEMMODEL 的 列数为不 确定性,或者说 有很多,要我怎么用
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
这样的来 写???

BernardSun 2008-07-18
  • 打赏
  • 举报
回复

declare @T table([EmpCode] varchar(20),[Quantity] int,[ItemModel] varchar(20))
Insert @T
select '张三',300,'A' union all
select '李四',150,'A' union all
select '赵五',200,'B'

select EmpCode,
A =sum(case when ItemModel='A' then Quantity else 0 end),
B =sum(case when ItemModel='B' then Quantity else 0 end)
from @T
group by EmpCode
liangCK 2008-07-18
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080614/17/22e73f33-f071-46dc-b9bf-321204b1656f.html
liangCK 2008-07-18
  • 打赏
  • 举报
回复
又是行转列....

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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