如何根据DB2数据库中提到的复杂条件编写SQL查询

时间:2017-01-13 12:42:47

标签: sql database db2

表名是: RU_OUTPUT

数据是:

id  rudef   value   jobid   st_dt   
-----------------------------------------------------
8   23      200     32      1481585920      
9   24      2       32      1481989201      
12  24      3       32      1481547920      
10  26      50      32      1481544560      
13  26      89      32      1481545920      
14  24      69      32      1481548920  
15  23      56      32      1486899920
16  29      896     32      1486789920

预期结果摘要: 首先我们必须找到所有超过1的rudef, 在那之后对于所有那些粗鲁的我们必须找到最后和第二个最后一个记录值作为不同的列只有当lastvalue> secondlastvalue。

  

(最后一条记录将根据该记录的id假设,即   rudef' 24'最后一条记录将是id = 14和倒数第二条记录   其中id = 12)

预期结果

rudef   lastvalue  secondlastvalue
24      69         3
26      89         50
  

注意:尽管rudef' 23' count是2但是它们的lastvalue是56,小于倒数第二个值200,因此不会被考虑。

3 个答案:

答案 0 :(得分:3)

在大多数数据库中,您可以使用条件聚合和ANSI标准foo<int>(1)函数:

row_number()

这假设select rudef, max(case when seqnum = 1 then value end) as last_value, max(case when seqnum = 2 then value end) as second_last_value from (select t.*, row_number() over (partition by rudef order by id desc) as seqnum from t ) t group by rudef having max(case when seqnum = 1 then value end) > max(case when seqnum = 2 then value end); 可用于确定最后一个值。 (如果应该使用其他列,只需更改id。)

我应该注意,您不必检查与order by匹配的两个值。如果只有一个匹配项,那么rudef表达式将为max()并且NULL条款失败。

答案 1 :(得分:1)

试试这个。但是,使用相同逻辑23拒绝26的逻辑也不会出现在预期输出中。无论如何,您可以调整内部查询以获得预期的输出。我在Oracle中尝试过,它正在给出预期的输出。

SELECT *
FROM
  ( SELECT rudef,
           max(value) AS lastvalue,
           min(value) AS secondlastvalue
   FROM
     ( SELECT t.*,
              row_number() over (partition BY rudef
                                 ORDER BY value DESC) AS val_rank
      FROM
        ( SELECT r.*,
                 row_number() over (partition BY rudef
                                    ORDER BY id DESC) AS rnk
         FROM RU_OUTPUT r) t
      WHERE rnk <=2 ) s
   WHERE rnk=val_rank
   GROUP BY rudef)
WHERE lastvalue<>secondlastvalue

输出

+-------+-----------+-----------------+
| RUDEF | LASTVALUE | SECONDLASTVALUE |
+-------+-----------+-----------------+
|    24 |        69 |               3 |
|    26 |        89 |              50 |
+-------+-----------+-----------------+

答案 2 :(得分:1)

检查以下查询是否适用于SQL Server或Oracle。

        with CTE as
        (
        select * from (
        select * from (
        select *,row_number() over(partition by rudef order by r desc) as Rnk
        from
        ( select *,row_number() over(order by (select 1))  as R from RU_OUTPUT)an

        )a where Rnk < 3  )b  -- change 4 for find 3rd last
        pivot
        (
         max(value)for rnk in ([2],[1])
        )
        as s

        )
        select c1.rudef,max(c1.[1]) as 'lastvalue',
        max(c1.[2]) as 'secondlastvalue'  from CTE C1   
        group by c1.rudef
        having max(c1.[2]) is not null and max(c1.[1]) is not null
        and max(c1.[1]) > max(c1.[2])   -- here condtion is lastvalue > secondlastvalue you can change it also
        order by rudef 

使用此查询,您还可以计算第3个最后值,依此类推。

max(c1.[1]) = lastvalue
max(c1.[2]) = 2nd lastvalue
max(c1.[3]) = 3rd lastvalue 
   ............and so on

如果您有任何疑虑,请告诉我们。