40分请叫一个sql 语句?在线等待!!!!!!

wgzy3250 2004-08-28 10:46:13
我有一表如下
itemno type qty
OT000206937292 I2 1146.89
OT000206937292 I3 169
OT0206937292 W1 -3259
OT0104600C01 I2 -1777
OT0104600C01 W1 -220
OT0109609X02 I2 -54
OT0109609X02 I3 300
OT0109724S01 I2 6
OT0109724S01 I3 350
现在要做成一报表表头如下
itemno Type I2 I3 W1
后三列就是按Type去sum数量,请问sql如何下?
...全文
118 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
azsoft 2004-08-29
  • 打赏
  • 举报
回复
动态列:
-----------

create table 表 (itemno varchar(30),type varchar(10),qty float)
insert into 表
select 'OT000206937292','I2',1146.89
union all select 'OT000206937292','I3',169
union all select 'OT0206937292','W1',-3259
union all select 'OT0104600C01','I2',-1777
union all select 'OT0104600C01','W1',-220
union all select 'OT0109609X02','I2',-54
union all select 'OT0109609X02','I3',300
union all select 'OT0109724S01','I2',6
union all select 'OT0109724S01','I3',350

declare @SQL varchar(8000)
Select @SQL='select itemno,type'
Select @SQL=@SQL+',sum(case when type='''+[type]+''' then qty else 0 end ) '+[type]+''
from (select distinct type from 表) as a
select @SQL=@SQL+' FROM 表 group by itemno,type order by itemno '

exec (@SQL)

drop table 表

结果:

itemno type I2 W1 I3
-------------------------------------------------------------

OT000206937292 I2 1146.8900000000001 0.0 0.0
OT000206937292 I3 0.0 169.0 0.0
OT0104600C01 I2 -1777.0 0.0 0.0
OT0104600C01 W1 0.0 0.0 -220.0
OT0109609X02 I2 -54.0 0.0 0.0
OT0109609X02 I3 0.0 300.0 0.0
OT0109724S01 I2 6.0 0.0 0.0
OT0109724S01 I3 0.0 350.0 0.0
OT0206937292 W1 0.0 0.0 -3259.0
azsoft 2004-08-29
  • 打赏
  • 举报
回复
try:

declare @SQL varchar(8000)
Select @SQL='select itemno,type'
Select @SQL=@SQL+',sum(case '+[type]+' when '+[type]+' then qty else 0 end ) '+[type]+'
from (select distinct type from 表) as a
select @SQL=@SQL+' FROM 表 group by itemno,type order by itemno '

exec (@SQL)
penghongxia 2004-08-29
  • 打赏
  • 举报
回复
select itemno,type,
sum(case when type='i2' then qty else 0 end) as I2,
sum(case when type='I3' then qty else 0 end) as I3,
sum(case when type='W1' then qty else 0 end) as W1

FROM tb
group by itemno,type
order by itemno
hglhyy 2004-08-29
  • 打赏
  • 举报
回复

declare @SQL varchar(8000)
Select @SQL='select itemno,type'
Select @SQL=@SQL+',sum(case '+[type]+' when '+[type]+' then qty else 0 end ) '+[type]+'
from (select distinct type from 表) as a
select @SQL=@SQL+' FROM 表 group by itemno,type order by itemno '

exec (@SQL)
yesterday2000 2004-08-28
  • 打赏
  • 举报
回复
select itemno,type,
sum(case when type='i2' then qty else 0 end) as I2,
sum(case when type='I3' then qty else 0 end) as I3,
sum(case when type='W1' then qty else 0 end) as W1

FROM tb
group by itemno,type
order by itemno
Andy__Huang 2004-08-28
  • 打赏
  • 举报
回复
select itemno,Type,
I2=sum(case type when 'I2' then qty else 0 end),
I3=sum(case type when 'I3' then qty else 0 end),
W1=sum(case type when 'W1' then qty else 0 end)
from table1
group by itemno,Type
zhangzs8896 2004-08-28
  • 打赏
  • 举报
回复
试试
select itemno, type
,I2=(SELECT TOP 1 SUM(qty) FROM 表 A WHERE A.TYPE=B.TYPE AND A.TYPE='I2' )
,I3=(SELECT TOP 1 SUM(qty) FROM 表 A WHERE A.TYPE=B.TYPE AND A.TYPE='I3')
,W1=(SELECT TOP 1 SUM(qty) FROM 表 A WHERE A.TYPE=B.TYPE AND A.TYPE='W1')
FROM 表 b

34,837

社区成员

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

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