求助 oracle 一条sql 语句, 急用 可以 加分

likaiyihou512 2013-09-22 10:42:03
有表如下:
字段 X Y Z A
x 10 1 2
x 10 2 3
x 11 1 4
x 11 2 6
y 15 5 1
y 15 6 4
y 16 5 2
y 16 6 8
求: 按字段X 分组,取 Y 最大的记录,
例如 要得到的结果是:
x 11 4 6,
y 16 2 8
...全文
516 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
likaiyihou512 2013-09-27
  • 打赏
  • 举报
回复
试下结贴能回复么
likaiyihou512 2013-09-27
  • 打赏
  • 举报
回复

create or replace view se_mark_examination_v as
with a as (select se.sys_user_id,se_exam_p.Get_Employee_Names(se.sys_user_id) as SYS_USER_NAME,se.se_paper_id,sp.paper_name,
       max(sea.attribute_10) as exam_num
from   se_examinee_answer sea,se_paper sp ,se_question sq,se_examinee se
where sea.se_examinee_id = se.id and sea.se_question_id = sq.id  and sp.id = se.se_paper_id
      and se.status = '3' and sp.reply_num is not null and sp.reply_grade is not null and sea.attribute_10 is not null
group by se.sys_user_id,se_exam_p.Get_Employee_Names(se.sys_user_id),se.se_paper_id,sp.paper_name),

b as (select se.sys_user_id,se_exam_p.Get_Employee_Names(se.sys_user_id) as SYS_USER_NAME,se.se_paper_id,sp.paper_name,
       sq.question_type,sum(sea.answer_of_score) as scores,sea.attribute_10
from   se_examinee_answer sea,se_paper sp ,se_question sq,se_examinee se,a
where sea.se_examinee_id = se.id and sea.se_question_id = sq.id  and sp.id = se.se_paper_id
      and a.sys_user_id = se.sys_user_id and a.se_paper_id = se.se_paper_id and a.exam_num = sea.attribute_10
      and se.status = '3' and sp.reply_num is not null and sp.reply_grade is not null and sea.attribute_10 is not null
group by se.sys_user_id,se_exam_p.Get_Employee_Names(se.sys_user_id),se.se_paper_id,sp.paper_name,sq.question_type,sea.attribute_10
)

select b.sys_user_id,b.SYS_USER_NAME,s.id as se_examinee_id,b.se_paper_id,b.paper_name,b.attribute_10 as exam_num,s.real_end_time as exam_start_time
       ,max(decode(b.question_type, '1', scores,0)) dx_scores1,--单选
       max(decode(b.question_type, '2', scores,0)) duox_scores2,--多选
       max(decode(b.question_type, '3', scores,0)) tk_scores3,--填空
       max(decode(b.question_type, '4', scores,0)) pd_scores4,--判断题
       max(decode(b.question_type, '5', scores,0)) jd_scores5--简单题
from b
left join se_examinee s on b.se_paper_id = s.se_paper_id and b.sys_user_id =s.sys_user_id
group by b.sys_user_id,b.SYS_USER_NAME,s.id,b.se_paper_id,b.paper_name,b.attribute_10,s.real_end_time
order by b.sys_user_id,b.se_paper_id,b.attribute_10

解决了,感谢大家的回答,及给予的思路,赋上解决sql给网友参考
likaiyihou512 2013-09-22
  • 打赏
  • 举报
回复
引用 6 楼 kuaileyangle 的回复:
最近学了个函数,来抛砖引玉一下:

WITH T AS
(SELECT 'x' X, 10 Y, 1 Z, 2 A
FROM DUAL
UNION ALL
SELECT 'x', 10, 2, 3
FROM DUAL
UNION ALL
SELECT 'x', 11, 1, 4
FROM DUAL
UNION ALL
SELECT 'x', 11, 2, 6
FROM DUAL
UNION ALL
SELECT 'y', 15, 5, 1
FROM DUAL
UNION ALL
SELECT 'y', 15, 6, 4
FROM DUAL
UNION ALL
SELECT 'y', 16, 5, 2
FROM DUAL
UNION ALL
SELECT 'y', 16, 6, 8 FROM DUAL)
SELECT X, MAX(Y), WMSYS.WM_CONCAT(A)
FROM (SELECT * FROM T WHERE Y IN (SELECT MAX(Y) FROM T GROUP BY X))
GROUP BY X;


接近楼主的要求了,但还不完美!

WMSYS.WM_CONCAT 开始也考虑了 用这个内部函数,但是 弄成了一个字段,前台显示 不好看。实在没有其他的方法的话考虑用这个函数
likaiyihou512 2013-09-22
  • 打赏
  • 举报
回复
引用 5 楼 lyliu602 的回复:
你用decode转换一下吧
decode(z,1,a)a
decode(z,2,a)b

select t.x,t.y,decode(t.z, '1', t.a) as A , decode(t.z, '2', t.a) as B
from (
select x,y,z,a from test where (x,y) in
(select x,max(to_number(y)) from test group by x)
--group by x,y
order by x,y ) t -- ,wmsys.wm_concat(A)
现在 2013-09-22
  • 打赏
  • 举报
回复
最近学了个函数,来抛砖引玉一下:

WITH T AS
 (SELECT 'x' X, 10 Y, 1 Z, 2 A
    FROM DUAL
  UNION ALL
  SELECT 'x', 10, 2, 3
    FROM DUAL
  UNION ALL
  SELECT 'x', 11, 1, 4
    FROM DUAL
  UNION ALL
  SELECT 'x', 11, 2, 6
    FROM DUAL
  UNION ALL
  SELECT 'y', 15, 5, 1
    FROM DUAL
  UNION ALL
  SELECT 'y', 15, 6, 4
    FROM DUAL
  UNION ALL
  SELECT 'y', 16, 5, 2
    FROM DUAL
  UNION ALL
  SELECT 'y', 16, 6, 8 FROM DUAL)
SELECT X, MAX(Y), WMSYS.WM_CONCAT(A)
  FROM (SELECT * FROM T WHERE Y IN (SELECT MAX(Y) FROM T GROUP BY X))
 GROUP BY X;
接近楼主的要求了,但还不完美!
无敌小二傻 2013-09-22
  • 打赏
  • 举报
回复
你用decode转换一下吧 decode(z,1,a)a decode(z,2,a)b
likaiyihou512 2013-09-22
  • 打赏
  • 举报
回复
引用 2 楼 lyliu602 的回复:
看看懂你的z列是不是没有用到还是写错了呢? select x,max(y),z,a group by x,z,a
我取得 是Z列 不同, x 11 1 4 x 11 2 6 你看着两列,我要得到:x 11 4 6
请叫我-雷人 2013-09-22
  • 打赏
  • 举报
回复
数据确实乱,不过语法大体和楼上一致
无敌小二傻 2013-09-22
  • 打赏
  • 举报
回复
看看懂你的z列是不是没有用到还是写错了呢? select x,max(y),z,a group by x,z,a
likaiyihou512 2013-09-22
  • 打赏
  • 举报
回复
自己顶一个,来个大神,给解决下
gaojiebao123 2013-09-22
  • 打赏
  • 举报
回复
在实际中有什么应用能说下不?
无敌小二傻 2013-09-22
  • 打赏
  • 举报
回复
引用 7 楼 likaiyihou512 的回复:
[quote=引用 5 楼 lyliu602 的回复:] 你用decode转换一下吧 decode(z,1,a)a decode(z,2,a)b
select t.x,t.y,decode(t.z, '1', t.a) as A , decode(t.z, '2', t.a) as B from ( select x,y,z,a from test where (x,y) in (select x,max(to_number(y)) from test group by x) --group by x,y order by x,y ) t -- ,wmsys.wm_concat(A)[/quote] select t.x,t.y,decode(t.z, '1', t.a) as A , decode(t.z, '2', t.a) as B from ( 改为: select t.x,t.y,decode(t.z, '1', t.a,null,t.a) as A , decode(t.z, '2', t.a,null,t.a) as B from (
u010412956 2013-09-22
  • 打赏
  • 举报
回复
引用 13 楼 likaiyihou512 的回复:
Z 字段来控制(最多五种),有值得就显示 值,没有的 填 零
最多五种的话,就这样写

WITH T AS
 (SELECT 'x' X, 10 Y, 1 Z, 2 A
    FROM DUAL
  UNION ALL
  SELECT 'x', 10, 2, 3
    FROM DUAL
  UNION ALL
  SELECT 'x', 11, 1, 4
    FROM DUAL
  UNION ALL
  SELECT 'x', 11, 2, 6
    FROM DUAL
  UNION ALL
  SELECT 'y', 15, 5, 1
    FROM DUAL
  UNION ALL
  SELECT 'y', 15, 6, 4
    FROM DUAL
  UNION ALL
  SELECT 'y', 16, 5, 2
    FROM DUAL
  UNION ALL
  SELECT 'y', 16, 6, 8
    FROM DUAL
  UNION ALL
  SELECT 'y', 16, 7, 9 FROM DUAL),
t1 as
 (select x, y, a, rn
    from (select x,
                 y,
                 a,
                 dense_rank() over(partition by x order by y desc) dr,
                 row_number() over(partition by x order by y desc) rn
            from t)
   where dr = 1)
select * from t1 pivot(max(a) for rn in(1, 2, 3, 4, 5))
needforskill 2013-09-22
  • 打赏
  • 举报
回复
select x,gy,a1,a2,a3,a4,a5 from
(
select x,gy
,lag(a,case when rb-1 >=0 then rb-1 else 999 end,0) over(partition by x,gy order by a) a1
,lag(a,case when rb-2 >=0 then rb-2 else 999 end,0) over(partition by x,gy order by a) a2
,lag(a,case when rb-3 >=0 then rb-3 else 999 end,0) over(partition by x,gy order by a) a3
,lag(a,case when rb-4 >=0 then rb-4 else 999 end,0) over(partition by x,gy order by a) a4
,lag(a,case when rb-5 >=0 then rb-5 else 999 end,0) over(partition by x,gy order by a) a5
,row_number() over( partition by x order by  rownum desc) xrb
from
(select X,y,max(Y) over(partition by x) gy,
 rank() over (partition by x,y order by a asc) rb
,a
from
(select 'x' X, 10 Y,1 Z,2 A from dual
union
select 'x' X, 10 Y,2 Z,3 A from dual
union
select 'x' X, 11 Y,1 Z,4 A from dual
union
select 'x' X, 11 Y,2 Z,6 A from dual
union
select 'x' X, 11 Y,1 Z,7 A from dual
union
select 'x' X, 11 Y,2 Z,8 A from dual
union
select 'x' X, 11 Y,2 Z,9 A from dual
union
select 'y' X, 15 Y,5 Z,1 A from dual
union
select 'y' X, 15 Y,6 Z,4 A from dual
union
select 'y' X, 16 Y,5 Z,2 A from dual
union
select 'y' X, 16 Y,5 Z,9 A from dual
union
select 'y' X, 16 Y,6 Z,8 A from dual))
where y=gy)
where xrb=1
这个少了嵌套,找不到的列置为0
needforskill 2013-09-22
  • 打赏
  • 举报
回复
select x,gy,a1,a2,a3,a4,a5 from
(
select x,gy
,lag(a,case when rb-1 >=0 then rb-1 else 999 end) over(partition by x,gy order by a) a1
,lag(a,case when rb-2 >=0 then rb-2 else 999 end) over(partition by x,gy order by a) a2
,lag(a,case when rb-3 >=0 then rb-3 else 999 end) over(partition by x,gy order by a) a3
,lag(a,case when rb-4 >=0 then rb-4 else 999 end) over(partition by x,gy order by a) a4
,lag(a,case when rb-5 >=0 then rb-5 else 999 end) over(partition by x,gy order by a) a5
,row_number() over( partition by x order by  rownum desc) xrb
from (select
x,gy,a,row_number() over(partition by x order by a asc) rb
from
(select X,y,max(Y) over(partition by Z) gy
,a
from
(select 'x' X, 10 Y,1 Z,2 A from dual
union
select 'x' X, 10 Y,2 Z,3 A from dual
union
select 'x' X, 11 Y,1 Z,4 A from dual
union
select 'x' X, 11 Y,2 Z,6 A from dual
union
select 'x' X, 11 Y,1 Z,7 A from dual
union
select 'x' X, 11 Y,2 Z,8 A from dual
union
select 'x' X, 11 Y,2 Z,9 A from dual
union
select 'y' X, 15 Y,5 Z,1 A from dual
union
select 'y' X, 15 Y,6 Z,4 A from dual
union
select 'y' X, 16 Y,5 Z,2 A from dual
union
select 'y' X, 16 Y,6 Z,8 A from dual))
where y=gy))
where xrb=1
用的分析函数有点多,而且还是嵌套,如果数据量大(千万级)的话,建议分几步作
likaiyihou512 2013-09-22
  • 打赏
  • 举报
回复
引用 14 楼 qq304213346 的回复:
with t as( select 'x' X, 10 Y,1 Z,2 A from dual union select 'x' X, 10 Y,2 Z,3 A from dual union select 'x' X, 11 Y,1 Z,4 A from dual union select 'x' X, 11 Y,2 Z,6 A from dual union select 'y' X, 15 Y,5 Z,1 A from dual union select 'y' X, 15 Y,6 Z,4 A from dual union select 'y' X, 16 Y,5 Z,2 A from dual union select 'y' X, 16 Y,6 Z,8 A from dual) select x,y, wmsys.wm_concat(a) from t where (x,y) in ( select x,max(y) from t group by x) group by x,y
如果A列 有存在 为null 的数据,使用 这个函数就不好使了
请叫我-雷人 2013-09-22
  • 打赏
  • 举报
回复
with t as( select 'x' X, 10 Y,1 Z,2 A from dual union select 'x' X, 10 Y,2 Z,3 A from dual union select 'x' X, 11 Y,1 Z,4 A from dual union select 'x' X, 11 Y,2 Z,6 A from dual union select 'y' X, 15 Y,5 Z,1 A from dual union select 'y' X, 15 Y,6 Z,4 A from dual union select 'y' X, 16 Y,5 Z,2 A from dual union select 'y' X, 16 Y,6 Z,8 A from dual) select x,y, wmsys.wm_concat(a) from t where (x,y) in ( select x,max(y) from t group by x) group by x,y
likaiyihou512 2013-09-22
  • 打赏
  • 举报
回复
Z 字段来控制(最多五种),有值得就显示 值,没有的 填 零
likaiyihou512 2013-09-22
  • 打赏
  • 举报
回复
实际上有5列
needforskill 2013-09-22
  • 打赏
  • 举报
回复
你是的意思是有多少条,后面就加多少列? 那这样的话就会出现,有些组有两条,有些组有五条,那到底是要多少列。 比如说如果最多条数的是五,后面加五列,对于那些只有两三条的分组,那些空列怎么处理 你要把你的需求再说明白点
加载更多回复(2)

17,086

社区成员

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

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