SQL Keep(dense_rank last order by)到H2数据库

时间:2015-06-17 01:48:47

标签: sql oracle h2

我有以下选择声明

SELECT c.car_id
MAX(m.mod_number) KEEP (DENSE_RANK LAST ORDER BY
DECODE(m.mod_number 'SP1', 10, 'SP2', 20, 'SP3', 30, 40))
FROM CAR c, MANUFACTURE m
WHERE c.car_type = m.car_type
AND m.make LIKE 'FOR%'
GROUP BY c.car_id;

我想将其更改为H2语法进行单元测试,但我只能使用解码。我不知道如何使用keep(dense_rank ...)进行修改我如何在保留此select语句的Oracle功能的同时做到这一点?

2 个答案:

答案 0 :(得分:2)

我认为这可行:

SELECT c.car_id,
       (CASE MAX(CASE WHEN m.mod_number = 'SP1' THEN 10
                      WHEN m.mod_number = 'SP2' THEN 20
                      WHEN m.mod_number = 'SP3' THEN 30
                      ELSE 40
                  END)
             WHEN 10 THEN 'SP1'
             WHEN 20 THEN 'SP2'
             WHEN 30 THEN 'SP3'
             ELSE MAX(CASE WHEN m.mod_number NOT IN ('SP1', 'SP2', 'SP3') THEN m.mod_number
                      END)
       END)
FROM CAR c JOIN
     MANUFACTURE m
     ON c.car_type = m.car_type
WHERE m.make LIKE 'FOR%'
GROUP BY c.car_id;

计算最大优先级,然后将值映射回原始值。

答案 1 :(得分:0)

最接近的等价物是这样的:

SELECT 
c.car_id
SUBSTRING(
    MAX(
        DECODE(m.mod_number 'SP1', '10', 'SP2', '20', 'SP3', '30', '40')) 
        || 
        TO_CHAR(m.mod_number)
    ),
3,100) NewField
FROM CAR c, MANUFACTURE m
WHERE c.car_type = m.car_type
AND m.make LIKE 'FOR%'
GROUP BY c.car_id;

KEEP是一种从不同的基础(顺序)中选择单个记录到您正在使用的实际记录集的方法。幸运的是,这个特定的查询确实不一定需要它。

在尝试转换为H2之前先在Oracle中尝试此操作