求一sql语句

xubangsong1982 2007-01-12 05:21:35
create table t(
year number(4) not null, --年度
s1 number, -- 一季度销售额
s2 number, -- 二季度销售额
s3 number, -- 三季度销售额
s4 number -- 四季度销售额
);
insert into t values(2001,120,130,100,140);
insert into t values(2002,180,130,180,140);
select * from t;
YEAR S1 S2 S3 S4
---------- ---------- ---------- ---------- ----------
2001 120 130 100 140
2002 180 130 180 140

(2)问题描述,如:
要得到各年度销售额最大的季度(同一年中销售额出现两个相同最大的,取最大季度),

(3)期望结果,如:
 year  max_season
 ------   ----------------
 2001      4
 2002      3
请问这个sql语句该怎么?
...全文
136 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
precipitant 2007-01-13
  • 打赏
  • 举报
回复
看看聚合函数 rank()
http://blog.csdn.net/precipitant/archive/2005/08/03/445022.aspx
夜雨山庄 2007-01-12
  • 打赏
  • 举报
回复
select t2.year, max(t3.month) as max_season
from
(
select t1.year max(t1.value) max_value
from
(
select year, s1 as value, "1" as season from t
union
select year, s2 as value, "2" as season from t
union
select year, s3 as value, "3" as season from t
union
select year, s4 as value, "4" as season from t
) t1
group by t1.year
) t2,
(
select year, s1 as value, "1" as season from t
union
select year, s2 as value, "2" as season from t
union
select year, s3 as value, "3" as season from t
union
select year, s4 as value, "4" as season from t
) t3
where
t2.year = t3.year
group by t2.year

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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