34,593
社区成员
发帖
与我相关
我的任务
分享
select * from tb a
where 子ID = (
select top 1 子ID from tb
where id=a.id
order by 子ID
)
--SQL2005
select ID ,子ID ,客户名, 客户订单号, 金额
from
(select *, tmp=row_number() over (partition by ID order by getdate())
from T
) A
where tmp=1
-----------------------------------------
--> 测试时间:2009-07-15
--> 我的淘宝:http://shop36766744.taobao.com/
--------------------------------------------------
if object_id('[tb]') is not null drop table [tb]
create table [tb]([ID] int,[子ID] int,[客户名] varchar(3),[客户订单号] int,[金额] int)
insert [tb]
select 1,1,'aaa',10001,1000 union all
select 1,2,'bbb',20002,2000 union all
select 1,3,'ccc',30004,1140 union all
select 2,1,'www',10100,3000 union all
select 2,2,'qqq',32000,4500
select * from [tb] t where not exists(select 1 from tb where T.id=id and T.子ID>子ID)
/*
ID 子ID 客户名 客户订单号 金额
----------- ----------- ---- ----------- -----------
1 1 aaa 10001 1000
2 1 www 10100 3000
(所影响的行数为 2 行)
*/
drop table TB
select * from tb a
where not exists (
select 1 from tb
where id=a.id
and 子ID <a.子ID
)