8

我有一个 Oracle 查询

select max(m.id),
       m.someId keep (DENSE_RANK FIRST ORDER BY m.UpdateDate desc) 
from MyTable m 
groupBy m.someId

对于这样的数据:

id UpdateDate someId
1  20-01-2012 10
2  20-01-2012 10
3  01-01-2012 10
4  10-02-2012 20
5  01-02-2012 20
6  01-04-2012 30

将返回给我这个:

2 10
4 20
6 30

因此,对于每个 someId,它都会搜索最新的 updateDate 并返回适当的id. (如果最新日期有多个 id,则需要最新的 id)。

但是对于 SQL Server,这个查询会以同样的方式工作吗?我是说这个建筑keep (dense_rank first order by ..)

4

5 回答 5

8

我认为您的特定查询不会运行 SQL Server。但是您可以通过以下方式获得相同的结果:

SELECT id, SomeId
FROM (  SELECT *, ROW_NUMBER() OVER(PARTITION BY someId ORDER BY UpdateDate DESC, id DESC) Corr
        FROM MyTable) A
WHERE Corr = 1
于 2012-05-25T15:20:27.127 回答
3

我返回并返回这个问题和答案。不幸的是,在某些情况下,使用“排名窗口函数”进行迁移变得非常复杂。这些情况是:

  1. Oracle查询的select部分中基于不同顺序的许多KEEP-DENSE_RANK结构
  2. 通过分组集/汇总分组

因此,我将在答案中添加其他信息。原始数据SQLFIDDLE:http ://sqlfiddle.com/#!6/e5c6d/6

1.读取oracle函数:

select max(m.id), m.someId keep (DENSE_RANK FIRST ORDER BY m.UpdateDate desc) 
from MyTable m 
groupBy m.someId

在那里我们选择组中最大的 m.id (someId, UpdateDate) 其中 UpdateDate 是组中最大的 (someId)

2.由于错误,直接方式不起作用:列“MyTable.UpdateDate”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。

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.现在让我们获取更复杂的表和更复杂的查询: ORACLE : http://sqlfiddle.com/#!4/c943c/23 SQL SERVER: http://sqlfiddle.com/#!6/dc7fb/1/ 0 (数据是预先生成的,在两个沙箱中是相同的 - 很容易比较结果)表:

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
)

甲骨文查询:

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# 编写。如果有人有兴趣,请与我联系...自定义聚合是此类问题的最佳解决方案,但就 varchar 长度而言,它并不是 unviersal。对于每个 varchar 长度,您将有义务创建“专业”聚合函数

于 2014-03-12T17:22:58.543 回答
2

那绝对有效。先尝试,再争论。当您有多个订单时,您可以这样做(在 Oracle 上制作的示例):

-- 这个有 keep dense_rank

WITH a AS (SELECT 1 s1, 4 s2, 'a' c,  10 g FROM dual UNION all
           SELECT 2 s1, 2 s2, 'b' c,  10 g FROM dual UNION ALL
           SELECT 3 s1, 1 s2, 'c' c,  20 g FROM dual UNION ALL
           SELECT 4 s1, 3 s2, 'd' c,  20 g FROM dual)
SELECT g,
       MAX(c) KEEP (DENSE_RANK FIRST ORDER BY s1) s1,
       MAX(c) KEEP (DENSE_RANK FIRST ORDER BY s2) s2
  FROM a
 GROUP BY g

-- 这个没有keep dense_rank

    WITH a AS (SELECT 1 s1, 4 s2, 'a' c,  10 g FROM dual UNION all
               SELECT 2 s1, 2 s2, 'b' c,  10 g FROM dual UNION ALL
               SELECT 3 s1, 1 s2, 'c' c,  20 g FROM dual UNION ALL
               SELECT 4 s1, 3 s2, 'd' c,  20 g FROM dual)
SELECT g,
       MAX(DECODE(s1, 1, c)) s1,
       MAX(DECODE(s2, 1, c)) s2
  FROM (SELECT g,c,
               ROW_NUMBER() OVER (PARTITION BY g ORDER BY s1) s1,
               ROW_NUMBER() OVER (PARTITION BY g ORDER BY s2) s2 
          FROM a) b
 GROUP BY g
于 2015-08-20T15:26:44.953 回答
1

SQL Server 不支持“keep”构造,因此需要使用子查询:

select m.*
from (select *, row_number() over (partition by m.someid ORDER BY m.UpdateDate desc) as seqnum
      from MyTable m
     ) m
where seqnum = 1

这会找到每个 m.id 的第一行,其中包含最新的 UpdateDate。然后它在外部查询中选择该行。请注意,您不需要使用此方法进行分组。

于 2012-05-25T15:25:01.330 回答
0

如果有人在 Postgres 中寻找 Oracle KEEP DENSE_RANK 模拟:

CREATE TABLE myt (
  "id" INTEGER,
  "update_date" timestamp,
  "some_id" INTEGER
);

INSERT INTO myt
  ("id", "update_date", "some_id")
VALUES
  ('1', '2012-01-20', '10'),
  ('2', '2012-01-20', '10'),
  ('3', '2012-01-01', '10'),
  ('4', '2012-10-02', '20'),
  ('5', '2012-01-02', '20'),
  ('6', '2012-01-04', '30');                         


select
    some_id, 
    (array_agg(id order by update_date desc, id desc))[1]
from myt
group by some_id
order by some_id

在此处输入图像描述

于 2019-04-11T04:26:12.663 回答