求一SQL语句

renshibubushiren 2011-04-21 03:38:37
有如下表
国家 公司 收入
a a1 100
a a2 200
a a3 300
b b1 150
b b2 300
b b3 450

想得到每个国家收入最高的公司,即如下结果

国家 公司 收入
a a3 300
b b3 450

请问该怎么写SQL语句呢,谢谢
...全文
111 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssqtjffcu 2011-04-21
  • 打赏
  • 举报
回复

with t as (
select 'a' 国家,'a1' 公司,'100' 收入 from dual
union all
select 'a','a2','200' from dual
union all
select 'a','a3','300' from dual
union all
select 'b','b1','150' from dual
union all
select 'b','b2','300' from dual
union all
select 'b','b3','450' from dual
)
select 国家, 公司, 收入
from (select 国家,
公司,
收入,
dense_rank() over(partition by 国家 order by 收入 desc) rn
from t) t1
where t1.rn <= 2 --前二名,前n名的话<=n即可
cutebear2008 2011-04-21
  • 打赏
  • 举报
回复

select 国家,公司,max(收入)from tb group by 国家
ssqtjffcu 2011-04-21
  • 打赏
  • 举报
回复

select * from (select 国家,收入,DENSE_RANk() over (partition by 国家 order by 收入 desc) rn from 收入表) t where t.rn<=2
renshibubushiren 2011-04-21
  • 打赏
  • 举报
回复
刚才帖子发错地方了,我要求一个能在PLSQL中执行的sql语句,请指教,谢谢
dawugui 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 renshibubushiren 的回复:]
如果要是求每个国家中,收入前两名的公司,该如何写呢?
[/Quote]

select t.* from tb t where 收入 in (select top 2 收入 from tb where 国家 = t.国家 order by 收入 desc)

--小F-- 2011-04-21
  • 打赏
  • 举报
回复
---------------------------------
-- Author: liangCK 小梁
-- Title : 查每个分组前N条记录
-- Date : 2008-11-13 17:19:23
---------------------------------

--> 生成测试数据: #T
IF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T
CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)
INSERT INTO #T
SELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALL
SELECT '002',1,'胡百敬','SQLServer2005性能调校','2008-03-22' UNION ALL
SELECT '003',1,'格罗夫Groff.J.R.','SQL完全手册','2009-07-01' UNION ALL
SELECT '004',1,'KalenDelaney','SQLServer2005技术内幕存储引擎','2008-08-01' UNION ALL
SELECT '005',2,'Alex.Kriegel.Boris.M.Trukhnov','SQL宝典','2007-10-05' UNION ALL
SELECT '006',2,'飞思科技产品研发中心','SQLServer2000高级管理与开发','2007-09-10' UNION ALL
SELECT '007',2,'胡百敬','SQLServer2005数据库开发详解','2008-06-15' UNION ALL
SELECT '008',3,'陈浩奎','SQLServer2000存储过程与XML编程','2005-09-01' UNION ALL
SELECT '009',3,'赵松涛','SQLServer2005系统管理实录','2008-10-01' UNION ALL
SELECT '010',3,'黄占涛','SQL技术手册','2006-01-01'

--SQL查询如下:

--按GID分组,查每个分组中Date最新的前2条记录


--1.字段ID唯一时:
SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)

--2.如果ID不唯一时:
SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)

--SQL Server 2005 使用新方法

--3.使用ROW_NUMBER()进行排位分组
SELECT ID,GID,Author,Title,Date
FROM
(
SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),*
FROM #T
) AS T
WHERE rid<=2

--4.使用APPLY
SELECT DISTINCT b.*
FROM #T AS a
CROSS APPLY
(
SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC
) AS b


--结果
/*

ID GID Author Title Date
---- ----------- ----------------------------- --------------------------------------- -----------------------
003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000
004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000
005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000
007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000
009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000
010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000

(6 行受影响)
*/
--小F-- 2011-04-21
  • 打赏
  • 举报
回复
select
distinct b.*
from
tb a
cross apply
(select top 2 * from tb where 国家=a.国家 order by 收入 desc)b
快溜 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 renshibubushiren 的回复:]
引用 4 楼 renshibubushiren 的回复:

如果要是求每个国家中,收入前两名的公司,该如何写呢?



如果要是求每个国家中,收入前两名的公司,该如何写呢?
[/Quote]
declare @t table (国家 varchar(1),公司 varchar(2),收入 int)
insert into @t
select 'a','a1',100 union all
select 'a','a2',200 union all
select 'a','a3',300 union all
select 'b','b1',150 union all
select 'b','b2',300 union all
select 'b','b3',450

select * from (select ROW_NUMBER() over(partition by 国家 order by 收入 desc) no,* from @t)a
where no<3

/*
no 国家 公司 收入
-------------------- ---- ---- -----------
1 a a3 300
2 a a2 200
1 b b3 450
2 b b2 300
qq623932737 2011-04-21
  • 打赏
  • 举报
回复
select a.GuoJia,a.GongSi,a.ShouRu from
a,(select GuoJia,max(ShouRu)as ShouRu from a group by GuoJia)as x
where a.ShouRu=x.ShouRu
renshibubushiren 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 renshibubushiren 的回复:]

如果要是求每个国家中,收入前两名的公司,该如何写呢?
[/Quote]


如果要是求每个国家中,收入前两名的公司,该如何写呢?
书生 2011-04-21
  • 打赏
  • 举报
回复
select max(国家) as 国家,max(公司) as 公司,max(收入) as 收入 from table
group by 国家,公司,收入
yuangang1011 2011-04-21
  • 打赏
  • 举报
回复
select 国家,min(公司),Max(收入) from tb group by 国家,收入
叶子 2011-04-21
  • 打赏
  • 举报
回复

declare @t table (国家 varchar(1),公司 varchar(2),收入 int)
insert into @t
select 'a','a1',100 union all
select 'a','a2',200 union all
select 'a','a3',300 union all
select 'b','b1',150 union all
select 'b','b2',300 union all
select 'b','b3',450

select * from @t t where 收入=(
select max(收入) from @t where 国家=t.国家)
order by 国家
/*
国家 公司 收入
---- ---- -----------
a a3 300
b b3 450
*/
renshibubushiren 2011-04-21
  • 打赏
  • 举报
回复
如果要是求每个国家中,收入前两名的公司,该如何写呢?
dawugui 2011-04-21
  • 打赏
  • 举报
回复
select t.* from tb t where 收入 = (select max(收入) from tb where 国家 = t.国家)

select t.* from tb t where not exists (select 1 from tb where 国家 = t.国家 and 收入 > t.收入)
快溜 2011-04-21
  • 打赏
  • 举报
回复
select * from tb a
where not exists(select 1 from tb where 国家=a.国家 and 收入>a.收入)
--or
select * from tb a
where 收入=(select max(收入) from tb where 国家=a.国家)
vivai2010 2011-04-21
  • 打赏
  • 举报
回复
select a.* from tb a where not exists (select 1 from tb where 国家=a.国家 and 收入>a.收入 )

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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