有一表
编号 数量
0002 66
0001 60
0005 110
0001 90
0002 80
0003 90
想从上表中得到下面编号表
0001
0001
0001
0002
0002
0003
0005
0005算法:
把每个编号数量相加除以50取整数即为该编号的条数。
如编号0001数量和为150,除以50取整数得3,故0001有三行。
谢谢各位!!
编号 数量
0002 66
0001 60
0005 110
0001 90
0002 80
0003 90
想从上表中得到下面编号表
0001
0001
0001
0002
0002
0003
0005
0005算法:
把每个编号数量相加除以50取整数即为该编号的条数。
如编号0001数量和为150,除以50取整数得3,故0001有三行。
谢谢各位!!
解决方案 »
- 日期可以分割做链接吗?
- 查询分析器执行2段脚本是顺序执行么?
- 一个小存储过程的问题,大家帮帮忙
- 关于连接sql server2000的一个ODBC配置的问题?希望大家给我讲讲
- 新手问题,存储过程的参数和其他字符,怎么连起来组成一个整体啊。
- sqlserver2000怎样为主键统一加上一个数字
- 安装SQL SERVER 2000以后如何创建数据库和表,望高手指点。
- 急,关于visual c++2005中的ado data 控件
- 怎样制作ASP+SQL SERVER 的打包程序
- 关于SQL中临时表的问题,稿分相送!
- mssql2000版数据库能否移植到mssql2005版?
- 跪求一存储过程(在线等待) 急用!!!
open c1
while(1=1)
begin
fetch c1 into @bh,@sl
if @@fetch_status <> 0
break
if @sl >=50
begin
insert into @temp(bh) values (@bh)
@sl = @sl -50
end
close c1
deallocate c1
占位问个小问题 ..有一值如果a为0的话想让他取Null值,应该怎么写,例:
isnull(case a when 0 then null else a end,b) --这语法不对,怎么改改
if object_id('tb') is not null drop table tb
go
create table tb (bh varchar(4),sl int)
insert into tb
select '0002',66 union all
select '0001',60 union all
select '0005',110 union all
select '0001',90 union all
select '0002',80 union all
select '0003',90----------------------------------
if object_id('tb1') is not null drop table tb1
go
create table tb1 (bh varchar(4))
declare @bh varchar(10), @sl intdeclare cur cursor for select bh,sl=sum(sl) from tb group by bhopen cur
fetch next from cur into @bh,@sl
while @@fetch_status=0
begin
declare @n int
set @n=1 while @n<=(@sl/50)
begin
insert tb1 select @bh
set @n=@n+1
end fetch next from cur into @bh,@sl
end
go
select * from tb1
bh
----
0001
0001
0001
0002
0002
0003
0005
0005(8 行受影响)
-- Author: T.O.P
-- Create date: 2009/11/27
-- Version: SQL SERVER 2005
-- =============================================
declare @tb table([a] varchar(4),[b] int)
insert @tb
select '0002',66 union all
select '0001',60 union all
select '0005',110 union all
select '0001',90 union all
select '0002',80 union all
select '0003',90select [a],sum([b])/50 as b into #t from @tb group by [a] ;with cte2 as
(
select * from #t
union all
select b.a, b.b-1 as b
from #t a inner join cte2 b on a.[a] = b.[a]
where b.b>1
)
select a from cte2 order by adrop table #t
--测试结果:
/*
a
----
0001
0001
0001
0002
0002
0003
0005
0005(8 row(s) affected)
*/
case a=0 then null end
case when a=0 then null else a end
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([number] varchar(4),[qty] int)
insert [tb]
select '0002',66 union all
select '0001',60 union all
select '0005',110 union all
select '0001',90 union all
select '0002',80 union all
select '0003',90
---查询---
select a.number
from
(
select number,sum(qty)/50 as cnt
from tb
group by number
) a
join master..spt_values b on b.type='P' and b.number between 1 and a.cnt---结果---
number
------
0001
0001
0001
0002
0002
0003
0005
0005(所影响的行数为 8 行)
-- Author : liangCK 梁爱兰
-- Comment: 小梁 爱 兰儿
-- Date : 2009-11-27 15:31:35
-------------------------------------
--> 生成测试数据: @tb
DECLARE @tb TABLE (编号 varchar(4),数量 int)
INSERT INTO @tb
SELECT '0002',66 UNION ALL
SELECT '0001',60 UNION ALL
SELECT '0005',110 UNION ALL
SELECT '0001',90 UNION ALL
SELECT '0002',80 UNION ALL
SELECT '0003',90--SQL查询如下:SELECT A.编号
FROM (SELECT 编号,SUM(数量)/50 AS flag FROM @tb GROUP BY 编号) AS A
JOIN master.dbo.spt_values AS B
ON B.type = 'P' AND number < flag
declare @table table([编号] varchar(4),[数量] dec(9,2))
insert @table
select '0002',66 union all
select '0001',60 union all
select '0005',110 union all
select '0001',90 union all
select '0002',80 union all
select '0003',90
select r.编号
from (select 编号,cast(sum(数量)/50 as int) as 数量 from @table group by 编号) r
JOIN master.dbo.spt_values as t
on t.type = 'P' and number < 数量
--结果
------------------------
0001
0001
0001
0002
0002
0003
0005
0005