select size,qty=case when size='m' then qty when size='s' then qty when size='L' then sum(qty)/(select count(size) from table1 where size='L') end from table1
或者
select size,qty from table1 where size='m'
union all
select size,qty from table1 where size='s'
union all
select size,qty=sum(qty)/(select count(size) from table1 where size='L') from table1 where size='L'
或者
select size,qty from table1 where size='m'
union all
select size,qty from table1 where size='s'
union all
select size,qty=sum(qty)/(select count(size) from table1 where size='L') from table1 where size='L'
解决方案 »
- 关于SQL2数据表之间的链接
- 存储过程 时间作为参数问题
- 如何连接sql2005?
- 可以从A表的字段内容复制到B表的某字段内容去吗?
- 如果对方电脑能PING通我的服务器的IP,并有数据库的用户名是否就可以访问我的服务器数据库了?
- 邹捷,http://expert.csdn.net/Expert/topic/2925/2925491.xml?temp=.1438715的帖子你也不能解答吗?只是个概念问题啊!
- SQL查询的问题
- 使用SQL2000导入功能EXCEL文件数据时的问题.
- 非常着急的问题,请高手快来帮忙啊。
- 在用ADOquery控件参数化查询SQL SERVER数据库时,怎样给参数赋值?
- 关于SET IDENTITY_INSERT的问题
- 请教一个存储过程
drop table t_chai
if object_id('dbo.f_rlchai') is not null
drop function dbo.f_rlchai
go
create table t_chai(SIZE_ch varchar(10),QTY int)
insert t_chai
select 'M',400 union all
select 'S',500 union all
select 'L',1500
select * from t_chai
go
create function dbo.f_rlchai(@size varchar(10))
returns @t table (SIZE_ch varchar(10),QTY int)
as
begin
declare @qty int,@qtynow int
select @qty=QTY from t_chai where SIZE_ch=@size
while @qty>500
begin
set @qtynow=500
insert @t select @size,@qtynow
set @qty=@qty-500
end
insert @t select @size,@qty
return
end
go
declare @tt table(SIZE_ch varchar(10),QTY int)
select dbo.f_chai(SIZE_ch) into @tt from t_chai where QTY>500)
select * from t_chai where QTY<=500
union all
select * from @tt
SIZE QTY
M 400
S 500
L 1500 If qty> 500, 则进行拆分, 要得如下结果, 求最简单写法(能否不用循环)
SIZE QTY
M 400
S 500
L 500
L 500
L 500 我还在调试JL99000兄的
这样不就可以了?如果你的SIZE很多的话,可以用动态行列转换