求一select 语句 ,谢谢,我都弄了一上午了也没出来.

gansilian 2007-11-29 04:18:34
cUserName cBatch cTypeId cFlag sum1 cDateTime
anfujing 0710080001 信息制作 成功 15 2007-11-29 15:08:41.043
anfujing 0710080001 信息制作 失败 70 2007-11-29 15:08:41.077
anfujing 0710300009 ceb制作 成功 46 2007-11-29 15:08:41.090
anfujing 0710300009 ceb制作 失败 10 2007-11-29 15:08:41.090
c006 0711020000 信息制作 成功 169 2007-11-29 15:08:41.090
c006 0711020000 信息制作 失败 1 2007-11-29 15:08:41.107

想得到如下结果:
cUserName cBatch 信息制作成功 信息制作失败 ceb制作成功 ceb制作失败
anfujing 0710080001 15 70 0 0
anfujing 0710300009 0 0 46 10
c006 0711020000 169 1 0 0
请教各位高手,我是无能为力了
先谢谢各位了
...全文
102 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
gansilian 2007-11-29
  • 打赏
  • 举报
回复
结贴了,
gansilian 2007-11-29
  • 打赏
  • 举报
回复
哈哈,这里的高手太历害了,看来以后得常来了,向各位高位学习
gansilian 2007-11-29
  • 打赏
  • 举报
回复
ok,成了,太谢谢了
不过能否问一下为什么要加max啊
gansilian 2007-11-29
  • 打赏
  • 举报
回复
噢,我再试看啊
-狙击手- 2007-11-29
  • 打赏
  • 举报
回复
declare @tb table(cUserName char(10),cBatch  char(1),cTypeId varchar(10),cFlag varchar(10),sum1 int)



select cusername,cbatch,
max(case when ctypeid = '信息制作' and cflag = '成功' then sum1 else 0 end) as '信息制作成功',
max(case when ctypeid = '信息制作' and cflag = '失败' then sum1 else 0 end) as '信息制作失败',
max(case when ctypeid = 'ceb制作' and cflag = '成功' then sum1 else 0 end) as 'ceb制作成功',
max(case when ctypeid = 'ceb制作' and cflag = '失败' then sum1 else 0 end) as 'ceb制作失败'
from @tb
group by cusername,cbatch

/*

cusername cbatch 信息制作成功 信息制作失败 ceb制作成功 ceb制作失败
---------- ------ ----------- ----------- ----------- -----------

(所影响的行数为 0 行)

*/
云中客 2007-11-29
  • 打赏
  • 举报
回复
也就是每个case的后面需要增加end
云中客 2007-11-29
  • 打赏
  • 举报
回复
但是select cusername,cbatch,
max(case when ctypeid = '信息制作' and cflag = '成功' then sum1 else 0) as '信息制作成功',
max(case when ctypeid = '信息制作' and cflag = '失败' then sum1 else 0) as '信息制作失败',
max(case when ctypeid = 'ceb制作' and cflag = '成功' then sum1 else 0) as 'ceb制作成功',
max(case when ctypeid = 'ceb制作' and cflag = '失败' then sum1 else 0) as 'ceb制作失败'
from table
group by cusername,cbatch
---------------------------------------------------------------------------------------------
举一个例子:
max(case when ctypeid = '信息制作' and cflag = '成功' then sum1 else 0) as '信息制作成功'
修改为:
max(case when ctypeid = '信息制作' and cflag = '成功' then sum1 else 0 end) as '信息制作成功'
-狙击手- 2007-11-29
  • 打赏
  • 举报
回复
不好意思,少一个end


select   cusername,cbatch, 
max(case when ctypeid = '信息制作' and cflag = '成功' then sum1 else 0 end) as '信息制作成功',
max(case when ctypeid = '信息制作' and cflag = '失败' then sum1 else 0 end) as '信息制作失败',
max(case when ctypeid = 'ceb制作' and cflag = '成功' then sum1 else 0 end) as 'ceb制作成功',
max(case when ctypeid = 'ceb制作' and cflag = '失败' then sum1 else 0 end) as 'ceb制作失败'
from table
group by cusername,cbatch
dawugui 2007-11-29
  • 打赏
  • 举报
回复
我说的丫子是1,2楼的.

发完后才看到有其他人插对了.不好意思.
playwarcraft 2007-11-29
  • 打赏
  • 举报
回复
谢谢,
但是select cusername,cbatch,
max(case when ctypeid = '信息制作' and cflag = '成功' then sum1 else 0) as '信息制作成功',
max(case when ctypeid = '信息制作' and cflag = '失败' then sum1 else 0) as '信息制作失败',
max(case when ctypeid = 'ceb制作' and cflag = '成功' then sum1 else 0) as 'ceb制作成功',
max(case when ctypeid = 'ceb制作' and cflag = '失败' then sum1 else 0) as 'ceb制作失败'
from table
group by cusername,cbatch

这个语句报错

======================================

as 后面的欄位名,不需要加'',保險起見,加上[]
dawugui 2007-11-29
  • 打赏
  • 举报
回复
行列转换,楼上丫子已经写出.本龟不再写了.帖个我整理的.
我这里很暖和,短衣短裤.爽啊.

/*
普通行列转换(2007-11-18于海南三亚)

假设有张学生成绩表(tb)如下:
Name Subject Result
张三 语文  74
张三 数学  83
张三 物理  93
李四 语文  74
李四 数学  84
李四 物理  94
*/

-------------------------------------------------------------------------
/*
想变成
姓名 语文 数学 物理
---------- ----------- ----------- -----------
李四 74 84 94
张三 74 83 93
*/

create table tb
(
Name varchar(10) ,
Subject varchar(10) ,
Result int
)

insert into tb(Name , Subject , Result) values('张三' , '语文' , 74)
insert into tb(Name , Subject , Result) values('张三' , '数学' , 83)
insert into tb(Name , Subject , Result) values('张三' , '物理' , 93)
insert into tb(Name , Subject , Result) values('李四' , '语文' , 74)
insert into tb(Name , Subject , Result) values('李四' , '数学' , 84)
insert into tb(Name , Subject , Result) values('李四' , '物理' , 94)
go

--静态SQL,指subject只有语文、数学、物理这三门课程。
select name 姓名,
max(case subject when '语文' then result else 0 end) 语文,
max(case subject when '数学' then result else 0 end) 数学,
max(case subject when '物理' then result else 0 end) 物理
from tb
group by name
/*
姓名 语文 数学 物理
---------- ----------- ----------- -----------
李四 74 84 94
张三 74 83 93
*/

--动态SQL,指subject不止语文、数学、物理这三门课程。
declare @sql varchar(8000)
set @sql = 'select Name as ' + '姓名'
select @sql = @sql + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']'
from (select distinct Subject from tb) as a
set @sql = @sql + ' from tb group by name'
exec(@sql)
/*
姓名 数学 物理 语文
---------- ----------- ----------- -----------
李四 84 94 74
张三 83 93 74
*/

-------------------------------------------------------------------
/*加个平均分,总分
姓名 语文 数学 物理 平均分 总分
---------- ----------- ----------- ----------- -------------------- -----------
李四 74 84 94 84.00 252
张三 74 83 93 83.33 250
*/

--静态SQL,指subject只有语文、数学、物理这三门课程。
select name 姓名,
max(case subject when '语文' then result else 0 end) 语文,
max(case subject when '数学' then result else 0 end) 数学,
max(case subject when '物理' then result else 0 end) 物理,
cast(avg(result*1.0) as decimal(18,2)) 平均分,
sum(result) 总分
from tb
group by name
/*
姓名 语文 数学 物理 平均分 总分
---------- ----------- ----------- ----------- -------------------- -----------
李四 74 84 94 84.00 252
张三 74 83 93 83.33 250
*/

--动态SQL,指subject不止语文、数学、物理这三门课程。
declare @sql1 varchar(8000)
set @sql1 = 'select Name as ' + '姓名'
select @sql1 = @sql1 + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']'
from (select distinct Subject from tb) as a
set @sql1 = @sql1 + ' , cast(avg(result*1.0) as decimal(18,2)) 平均分,sum(result) 总分 from tb group by name'
exec(@sql1)
/*
姓名 数学 物理 语文 平均分 总分
---------- ----------- ----------- ----------- -------------------- -----------
李四 84 94 74 84.00 252
张三 83 93 74 83.33 250
*/

drop table tb

---------------------------------------------------------
---------------------------------------------------------
/*
如果上述两表互相换一下:即

姓名 语文 数学 物理
张三 74  83  93
李四 74  84  94

想变成
Name Subject Result
---------- ------- -----------
李四 语文 74
李四 数学 84
李四 物理 94
张三 语文 74
张三 数学 83
张三 物理 93
*/

create table tb1
(
姓名 varchar(10) ,
语文 int ,
数学 int ,
物理 int
)

insert into tb1(姓名 , 语文 , 数学 , 物理) values('张三',74,83,93)
insert into tb1(姓名 , 语文 , 数学 , 物理) values('李四',74,84,94)

select * from
(
select 姓名 as Name , Subject = '语文' , Result = 语文 from tb1
union all
select 姓名 as Name , Subject = '数学' , Result = 数学 from tb1
union all
select 姓名 as Name , Subject = '物理' , Result = 物理 from tb1
) t
order by name , case Subject when '语文' then 1 when '数学' then 2 when '物理' then 3 when '总分' then 4 end

--------------------------------------------------------------------
/*加个平均分,总分
Name Subject Result
---------- ------- --------------------
李四 语文 74.00
李四 数学 84.00
李四 物理 94.00
李四 平均分 84.00
李四 总分 252.00
张三 语文 74.00
张三 数学 83.00
张三 物理 93.00
张三 平均分 83.33
张三 总分 250.00
*/

select * from
(
select 姓名 as Name , Subject = '语文' , Result = 语文 from tb1
union all
select 姓名 as Name , Subject = '数学' , Result = 数学 from tb1
union all
select 姓名 as Name , Subject = '物理' , Result = 物理 from tb1
union all
select 姓名 as Name , Subject = '平均分' , Result = cast((语文 + 数学 + 物理)*1.0/3 as decimal(18,2)) from tb1
union all
select 姓名 as Name , Subject = '总分' , Result = 语文 + 数学 + 物理 from tb1
) t
order by name , case Subject when '语文' then 1 when '数学' then 2 when '物理' then 3 when '平均分' then 4 when '总分' then 5 end

drop table tb1
gansilian 2007-11-29
  • 打赏
  • 举报
回复
高手你还在吗?
gansilian 2007-11-29
  • 打赏
  • 举报
回复
Incorrect syntax near ')'.
Gangzai1983 2007-11-29
  • 打赏
  • 举报
回复
LS正解!!!做人要厚道!!
gansilian 2007-11-29
  • 打赏
  • 举报
回复
谢谢,
但是select cusername,cbatch,
max(case when ctypeid = '信息制作' and cflag = '成功' then sum1 else 0) as '信息制作成功',
max(case when ctypeid = '信息制作' and cflag = '失败' then sum1 else 0) as '信息制作失败',
max(case when ctypeid = 'ceb制作' and cflag = '成功' then sum1 else 0) as 'ceb制作成功',
max(case when ctypeid = 'ceb制作' and cflag = '失败' then sum1 else 0) as 'ceb制作失败'
from table
group by cusername,cbatch

这个语句报错
-狙击手- 2007-11-29
  • 打赏
  • 举报
回复
如果cTypeId数据不定,可以改成动态SQL


给你看一个例子,写得太长,手冷

要做一张难度较大的报表,请大家帮写组MS SQL语句,情况如下

Info 表(说明:Info 表记录为动态添加Code字段内容不会重复,可能还会添加更多)
Code IncDes
1009 天染公司
1005 海丰公司
1023 采诗公司

MaInfo 表(说明:MaInfo 表为库存表)
Code Barcode MaterialsCn number
1009 1111111 可口可乐 10
1009 1111112 芬达可乐 12
1009 1111113 花生果 15
1005 1111112 芬达可乐 20
1005 1111114 出前一丁 30
1023 1111112 芬达可乐 22
1005 1111113 花生果 40

SaleDetail 表(说明:SaleDetail 表为零售表)
Code Barcode MaterialsCn sellnum1
1009 1111111 可口可乐 2
1005 1111114 出前一丁 3
1023 1111112 芬达可乐 1

SaleOrder 表(说明:SaleOrder 表为销售表)
Code Barcode MaterialsCn sellnum2
1009 1111111 可口可乐 2
1023 1111112 芬达可乐 1

要的结果如下
天染公司 海丰公司 采诗公司 .....(公司是动态的)
总销量 库存 总销量 库存 总销量 库存 销量合计 库存合计
1111111 可口可乐 4 10 0 0 0 0 4 10
1111112 芬达可乐 0 12 0 20 2 22 2 54
1111113 花生果 0 15 0 40 0 0 0 55
1111114 出前一丁 0 0 3 30 0 0 3 30

结果说明:(如在此看结果排列的不整齐,麻烦大家把结果COPY到记事本看)
以MaInfo 库存表的Barcode为唯一依据,重复的商品不列出,
并以MaInfo 库存表的Barcode和Code为唯一关系SaleDetail 表和SaleOrder 表,
Info 表的Code关联其它三张表,由于过程有点复杂,麻烦大伙看清结果帮写组MS SQL语句,3Q

if object_id('pubs..Info') is not null
drop table Info
go
create table Info(Code varchar(10),IncDes varchar(10))
insert into Info(Code,IncDes) values('1009', '天染公司')
insert into Info(Code,IncDes) values('1005', '海丰公司')
insert into Info(Code,IncDes) values('1023', '采诗公司')
go

if object_id('pubs..MaInfo') is not null
drop table MaInfo
go
create table MaInfo(Code varchar(10),Barcode varchar(10),MaterialsCn varchar(10),number int)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1009', '1111111', '可口可乐', 10)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1009', '1111112', '芬达可乐', 12)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1009', '1111113', '花生果 ', 15)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1005', '1111112', '芬达可乐', 20)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1005', '1111114', '出前一丁', 30)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1023', '1111112', '芬达可乐', 22)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1005', '1111113', '花生果 ', 40)
go

if object_id('pubs..SaleDetail') is not null
drop table SaleDetail
go
create table SaleDetail(Code varchar(10),Barcode varchar(10),MaterialsCn varchar(10),sellnum1 int)
insert into SaleDetail(Code,Barcode,MaterialsCn,sellnum1) values('1009', '1111111', '可口可乐', 2)
insert into SaleDetail(Code,Barcode,MaterialsCn,sellnum1) values('1005', '1111114', '出前一丁', 3)
insert into SaleDetail(Code,Barcode,MaterialsCn,sellnum1) values('1023', '1111112', '芬达可乐', 1)
go

if object_id('pubs..SaleOrder') is not null
drop table SaleOrder
go
create table SaleOrder(Code varchar(10),Barcode varchar(10),MaterialsCn varchar(10),sellnum2 int)
insert into SaleOrder(Code,Barcode,MaterialsCn,sellnum2) values('1009', '1111111', '可口可乐', 2)
insert into SaleOrder(Code,Barcode,MaterialsCn,sellnum2) values('1023', '1111112', '芬达可乐', 1)
go

select info.incdes,o.* into test from info,
(
select isnull(m.Code,n.code) code , isnull(m.Barcode,n.barcode) barcode, isnull(m.MaterialsCn,n.MaterialsCn) MaterialsCn ,isnull(m.number,0) number ,isnull(n.sellnum,0) sellnum from
(
select Code , Barcode , MaterialsCn , sum(number) number from MaInfo
group by Code , Barcode , MaterialsCn
) m
full join
(
select code , barcode , materialscn , sum(sellnum) sellnum from
(
select SaleDetail.Code,SaleDetail.Barcode,SaleDetail.MaterialsCn,SaleDetail.sellnum1 sellnum from SaleDetail
union all
select SaleOrder.Code,SaleOrder.Barcode,SaleOrder.MaterialsCn,SaleOrder.sellnum2 sellnum from SaleOrder
) t
group by code , barcode , materialscn
) n
on m.code = n.code and m.Barcode = n.Barcode and m.MaterialsCn = n.MaterialsCn
) o
where info.code = o.code

--select * from test

declare @sql varchar(8000)
set @sql = 'select Barcode,MaterialsCn '
select @sql = @sql + ' , sum(case when IncDes = ''' + IncDes + ''' then sellnum else 0 end) [' + IncDes + '总销量' + ']'+
' , sum(case when IncDes = ''' + IncDes + ''' then number else 0 end) [' + IncDes + '库存' + ']'
from (select distinct IncDes from test) as a
set @sql = @sql + ' ,sum(sellnum) 销量合计 , sum(number) 库存合计 from test group by Barcode,MaterialsCn order by barcode,materialscn'
exec(@sql)

drop table Info,MaInfo,SaleDetail,SaleOrder,test


Barcode MaterialsCn 采诗公司总销量 采诗公司库存 海丰公司总销量 海丰公司库存 天染公司总销量 天染公司库存 销量合计 库存合计
------- ----------- -------------- ------------ -------------- ------------ ----------- ----------- ----------- -----------
1111111 可口可乐 0 0 0 0 4 10 4 10
1111112 芬达可乐 2 22 0 20 0 12 2 54
1111113 花生果 0 0 0 40 0 15 0 55
1111114 出前一丁 0 0 3 30 0 0 3 30


-狙击手- 2007-11-29
  • 打赏
  • 举报
回复
select cusername,cbatch,
max(case when ctypeid = '信息制作' and cflag = '成功' then sum1 else 0) as '信息制作成功',
max(case when ctypeid = '信息制作' and cflag = '失败' then sum1 else 0) as '信息制作失败',
max(case when ctypeid = 'ceb制作' and cflag = '成功' then sum1 else 0) as 'ceb制作成功',
max(case when ctypeid = 'ceb制作' and cflag = '失败' then sum1 else 0) as 'ceb制作失败'
from table
group by cusername,cbatch

34,873

社区成员

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

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