不幸的是,在使用“排位窗口功能”的移民变得非常复杂的情况下,存在着几种情况。
- many KEEP-DENSE_RANK constructions in the select part of
Oracle query based on different orders
- grouping by grouping sets/rollups
Therefore I will add to the answer additional information.
Original data SQLFIDDLE: http://sqlfiddle.com/#!6/e5c6d/6
<强 > 1. 强 > 读取符元函数 :
select max(m.id), m.someId keep (DENSE_RANK FIRST ORDER BY m.UpdateDate desc)
from MyTable m
groupBy m.someId
我们在其中选择组中最大 m. id 的 m. id 值( 约Id, 更新日期), 更新日期最大, 组中最大( 约Id) 。
2. 直向前进路 doesn t working ,因为错误: Column My Table。 选中列表中更新日期无效, 因为它既不包含在总计函数中, 也不包含在分组中 。
SELECT FIRST_VALUE(id) OVER(PARTITION BY someId ORDER BY UpdateDate DESC, id DESC) first_in_orderedset , someId
FROM MyTable
GROUP BY someId
<强 > 3. 强 > 直直直前方的杂草是无效的
SELECT someId, MIN(first_in_orderedset)
FROM
( SELECT FIRST_VALUE(id) OVER(PARTITION BY someId ORDER BY UpdateDate DESC, id DESC) first_in_orderedset , someId
FROM MyTable ) t
GROUP BY someId;
<强>4. 强>交叉号适用:
SELECT grouped.someId, orderedSet.FirstUpdateDate, maxInSet.first_in_orderedset FROM
(
SELECT mt.someId
FROM MyTable mt
GROUP BY mt.someId
) grouped CROSS APPLY
(
SELECT top 1 mt2.UpdateDate as FirstUpdateDate
FROM MyTable mt2
WHERE mt2.someId=grouped.someId
ORDER BY UpdateDate desc
) orderedSet CROSS APPLY
(
SELECT max(mt3.id) as first_in_orderedset
FROM MyTable mt3
WHERE mt3.someId=grouped.someId and mt3.UpdateDate=orderedSet.FirstUpdateDate
) maxInSet;
5. Now lets get the more complex table and more complex query:
ORACLE : http://sqlfiddle.com/#!4/c943c/23
SQL SERVER: http://sqlfiddle.com/#!6/dc7fb/1/0
(data is pregenerated and it is the same in both sandboxes - it is easy to compare results)
Table:
CREATE TABLE AlarmReports (
id int PRIMARY KEY,
clientId int, businessAreaId int , projectId int, taskId int,
process1Spent int, process1Lag int, process1AlarmRate varchar2(1) null,
process2Spent int, process2Lag int, process2AlarmRate varchar2(1) null,
process3Spent int, process3Lag int, process3AlarmRate varchar2(1) null
)
Oracle 查询 :
SELECT clientId, businessAreaId, projectId,
sum(process1Spent),
sum(process2Spent),
sum(process3Spent),
MIN(process1AlarmRate) KEEP (DENSE_RANK FIRST ORDER BY process1Lag DESC),
MIN(process2AlarmRate) KEEP (DENSE_RANK FIRST ORDER BY process2Lag DESC),
MIN(process3AlarmRate) KEEP (DENSE_RANK FIRST ORDER BY process3Lag DESC)
FROM AlarmReports
GROUP BY GROUPING SETS ((),(clientId),(clientId, projectId),(businessAreaId),(clientId,businessAreaId))
SQL 查询 :
(to be continued)
实际上,我计划把我的定制总合 写在c##。 如果有人感兴趣, 请联系我... 定制总合是解决这类问题的最好办法... 但对于每个纵曲长度来说,它不是没有生命力的。 您必须创建“ 专门化的” 括号函数 。