求SQL。好像有人问过,看过又忘了。。

DavidNoWay 2006-11-01 04:40:34

create table t1 ( a char(10),b char(10)
)
insert into t1
select '2006-4','100'
union all
select '2006-4','101'
union all select '2006-4','102'
union all select '2006-4','103'
union all select '2006-5','105'
union all select'2006-5','106'

drop table t1
a b
2006-4 100
2006-4 101
2006-4 102
2006-4 103
2006-5 105
2006-5 106
要求以下:a中的头两条记录

2006-4 100
2006-4 101
2006-5 105
2006-5 106

...全文
219 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenjunjarysky 2006-11-01
  • 打赏
  • 举报
回复
mark!
dawugui 2006-11-01
  • 打赏
  • 举报
回复
select top 50 percent
改为top 2即可.
dawugui 2006-11-01
  • 打赏
  • 举报
回复
示例数据(表a):
area cust money
--------------
A 123 20.0
A 159 20.0
A 456 25.0
A 483 30.0
A 789 40.0
A 597 50.0
B 147 10.0
B 258 20.0
B 369 25.0
B 384 30.0

希望得到的如下结果
area cust money
--------------
A 483 30.0
A 789 40.0
A 597 50.0
B 369 25.0
B 384 30.0

现有表a,想得到表a中各地区(area)的商户(cust)交易金额(money)排该地区里面前百分之50%的记录.
即要:
地区A中金额前百分之50%
地区B中金额前百分之50%
....C..............50%
....D..............50%
......................

CREATE TABLE #a (
[area] [char] (10),
[cust] [char] (10),
[money] [numeric](10, 1) NULL
)

insert into #a(area,cust,money) values('A','123',20.0)
insert into #a(area,cust,money) values('A','159',20.0)
insert into #a(area,cust,money) values('A','456',25.0)
insert into #a(area,cust,money) values('A','483',30.0)
insert into #a(area,cust,money) values('A','789',40.0)
insert into #a(area,cust,money) values('A','597',50.0)
insert into #a(area,cust,money) values('B','147',10.0)
insert into #a(area,cust,money) values('B','258',20.0)
insert into #a(area,cust,money) values('B','369',25.0)
insert into #a(area,cust,money) values('B','384',30.0)

select * from #a t
where cust in
(
select top 50 percent cust from #a where area=t.area order by money desc
)

drop table #a

//结果
area cust money
---------- ---------- ------------
A 483 30.0
A 789 40.0
A 597 50.0
B 369 25.0
B 384 30.0

(所影响的行数为 5 行)

汤昵 2006-11-01
  • 打赏
  • 举报
回复
这还是有一个问题呀,如果B中有相同的列呢,A就有三列显示出来

create table t1 ( a char(10),b char(10))
insert into t1 select '2006-4','100'
union all select '2006-4','101'
union all select '2006-4','101'
union all select '2006-4','103'
union all select '2006-5','105'
union all select'2006-5','106'

select a.* from t1 a where a.b in(select top 2 b from t1 where a=a.a order by b )


2006-4 100
2006-4 101
2006-4 101
2006-5 105
2006-5 106
点点星灯 2006-11-01
  • 打赏
  • 举报
回复
create table t1 ( a char(10),b char(10))
insert into t1 select '2006-4','100'
union all select '2006-4','101'
union all select '2006-4','102'
union all select '2006-4','103'
union all select '2006-5','105'
union all select'2006-5','106'
go
select * from t1 a where 2>(select count(*) from t1 b where a.a=b.a and a.b>b.b)
go

drop table t1
go
冷箫轻笛 2006-11-01
  • 打赏
  • 举报
回复
select * from t1 tt
where b in (select top 2 b from t1 where a = tt.a)
子陌红尘 2006-11-01
  • 打赏
  • 举报
回复
select a.* from t1 a where a.b in(select top 2 b from t1 where a=a.a order by b)
DavidNoWay 2006-11-01
  • 打赏
  • 举报
回复
沙发

34,575

社区成员

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

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