27,579
社区成员
发帖
与我相关
我的任务
分享
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-07 14:19:55
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([班级ID] varchar(3),[人数] int)
insert [tb]
select '001',2 union all
select '002',3 union all
select '003',8
--------------开始查询--------------------------
select
班级ID,人数
from
(
select *,总人数=(select isnull(sum(人数),0) from tb where [班级ID]<t.[班级ID] ) from [tb] t
) t
where
总人数<10
----------------结果----------------------------
/* 班级ID 人数
---- -----------
001 2
002 3
003 8
(3 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-07 14:19:55
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([班级ID] varchar(3),[人数] int)
insert [tb]
select '001',2 union all
select '002',3 union all
select '003',8
--------------开始查询--------------------------
select
*
from
(
select *,总人数=(select isnull(sum(人数),0) from tb where [班级ID]<t.[班级ID] ) from [tb] t
) t
where
总人数<10
----------------结果----------------------------
/* 班级ID 人数 总人数
---- ----------- -----------
001 2 0
002 3 2
003 8 5
(3 行受影响)
*/
-->Title:生成測試數據
-->Author:pl_mm(风吹屁屁凉~)
-->Date :2009-09-07 14:15:45
if not object_id('tb') is null
drop table tb
Go
Create table tb([班级ID] nvarchar(3),[人数] int)
Insert tb
select N'001',2 union all
select N'002',3 union all
select N'003',8
Go
select * from tb where [班级ID]<=
(select top 1 [班级ID]
from tb t
where (select sum([人数])from tb where [班级ID]<=t.[班级ID])>10)
/*
班级ID 人数
---- -----------
001 2
002 3
003 8
(3 個資料列受到影響)
*/
create table T(班级ID char(03), 人数 int)
insert into T select '001', 2
insert into T select '002', 3
insert into T select '003', 8
declare @max int
set @max=10 --判断数字
select * from T as A
where isnull((select sum(人数) from T where 班级ID<A.班级ID),0) <= @max
drop table T
--> 测试数据:[t1]
if object_id('[t1]') is not null drop table [t1]
go
create table [t1]([班级ID] varchar(3),[人数] int)
insert [t1]
select '001',2 union all
select '002',3 union all
select '003',8
declare @i int
set @I=10
select [班级ID] , [人数] from
(
select *,总人数=(select isnull(sum(人数),0) from t1 where [班级ID]<t.[班级ID] ) from [t1] t
) tmp
where 总人数<@i
-------------------------------------------
-- Author : liangCK 小梁
-- Comment: 小梁 爱 兰儿
-- Date : 2009-09-07 14:17:54
-------------------------------------------
--> 生成测试数据: @tb
DECLARE @tb TABLE (班级ID VARCHAR(3),人数 INT)
INSERT INTO @tb
SELECT '001',2 UNION ALL
SELECT '002',3 UNION ALL
SELECT '003',8
--SQL查询如下:
SELECT * FROM @tb AS A
WHERE ISNULL((SELECT SUM(人数) FROM @tb WHERE 班级ID <A.班级ID),0) <= 10;
SELECT * FROM @tb AS A
WHERE ISNULL((SELECT SUM(人数) FROM @tb WHERE 班级ID <A.班级ID),0) <= 4;
/*
班级ID 人数
---- -----------
001 2
002 3
003 8
(3 row(s) affected)
班级ID 人数
---- -----------
001 2
002 3
(2 row(s) affected)
*/