[英]SQL select from subquery
I have an issue selecting from a subquery with this small demo data for example: 我在从子查询中选择具有此小演示数据的问题,例如:
ID CLASS_ID PROP STATUS ROW SELECTED STEP
12345679 1312 0 21 3 07-FEB-14 1
12345679 1312 0 21 1 26-FEB-14 -1
I want to get all the information (each column), the sql below works ok 我想获取所有信息(每列),下面的sql正常运行
SELECT ID,CLASS_ID,min(SELECTED) FROM ( SELECT... ) GROUP BY ID,CLASS_ID
ID CLASS_ID SELECTED
12345679 1312 07-FEB-14
But if I want other columns I must add them to the group statement else I get "not a group statement" thus making it obsolete, any ideas? 但是,如果我想要其他列,则必须将它们添加到group语句中,否则我将获得“ not a group statement”,从而使它过时,有什么想法吗?
You want to use the analytic function row_number()
: 您要使用分析功能
row_number()
:
select ID, CLASS_ID, PROP, STATUS, ROW, SELECTED
from (select s.*, row_number() over (partition by id, class_id
order by selected desc) as seqnum
from (<subquery>) s
) s
where seqnum = 1;
you want to get the minimum of selected
column based on group by ID
and class_id
with all other columns then you can use MIN() over partitioned by
function - 您想要基于组
ID
和所有其他列的class_id
获得最少的selected
列,则可以MIN() over partitioned by
函数MIN() over partitioned by
使用MIN() over partitioned by
-
SELECT ID,
CLASS_ID,
min(SELECTED) over (partition by ID,CLASS_ID order by 1) ,
col1 ...
FROM ( SELECT... )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.