需要以下情况的SQL查询

时间:2019-06-13 05:57:56

标签: sql oracle

我需要提出一个SQL查询,如果相应的“ seq”列在表2的“ Res”列中没有“ C”,那么它将从表1中检索最大的“ seq”列

场景:1

Table 1
+----+---------+
| Seq | Req    | 
+----+---------+
| 001 | X1     |
| 002 | X1     |
| 003 | X1     |
+----+---------+

Table2
+----+---------+--------+
|Seq | Req     | Res    | 
+----+---------+--------+
| 001| X1     |   D     | 
| 002| X1     |   D     |
| 003| X1     |   C     |
+----+---------+--------+

输出:由于最大序列(003)在Res中为'C',因此我不想在此处显示任何记录

场景:2

Table 1
+----+---------+
| Seq | Req    | 
+----+---------+
| 001 | X1     |
| 002 | X1     |
| 003 | X1     |
| 004 | X1     |
+----+---------+

Table2
+----+---------+--------+
|Seq | Req     | Res    | 
+----+---------+--------+
| 001| X1     |   D     | 
| 002| X1     |   D     |
| 003| X1     |   C     |
+----+---------+--------+

输出:我想显示表1中的记录“ 004,X1”,因为在Res列中没有针对表2中最大seq(004)的“ C”(在表2中没有针对最大seq-004的记录

场景:3

Table 1
+----+---------+
| Seq | Req    | 
+----+---------+
| 001 | X1     |
| 002 | X1     |
| 003 | X1     |
| 004 | X1     |
+----+---------+

Table2
+----+---------+--------+
|Seq | Req     | Res    | 
+----+---------+--------+
| 001| X1     |   D     | 
| 002| X1     |   D     |
| 003| X1     |   C     |
| 004| X1     |   K     |
+----+---------+--------+

输出:我想显示表1中的记录“ 004,X1”,因为在Res列中没有“ C”表示表2中的最大seq(004)。表2(004)中的最大Seq数是K

尝试使用内部联接和左联接,但是一个或另一个方案失败。 请帮助我

3 个答案:

答案 0 :(得分:1)

在下面尝试一下

with cte as
(
 select *,row_number() over(order by seq desc) rn
 from table1
)    
select cte.* 
from cte left join table2 t2 on cte.seq=t2.seq
where t2.res<>'C' and cte.rn=1

答案 1 :(得分:1)

Oracle设置

CREATE TABLE Table1 (SEQ, REQ) AS
  SELECT '001', 'X1' FROM DUAL UNION ALL
  SELECT '002', 'X1' FROM DUAL UNION ALL
  SELECT '003', 'X1' FROM DUAL UNION ALL
  SELECT '004', 'X1' FROM DUAL UNION ALL

  SELECT '001', 'X2' FROM DUAL UNION ALL
  SELECT '002', 'X2' FROM DUAL UNION ALL
  SELECT '003', 'X2' FROM DUAL UNION ALL

  SELECT '001', 'X3' FROM DUAL UNION ALL
  SELECT '002', 'X3' FROM DUAL UNION ALL
  SELECT '003', 'X3' FROM DUAL UNION ALL
  SELECT '004', 'X3' FROM DUAL UNION ALL

  SELECT '001', 'X4' FROM DUAL UNION ALL
  SELECT '002', 'X4' FROM DUAL UNION ALL
  SELECT '003', 'X4' FROM DUAL UNION ALL
  SELECT '004', 'X4' FROM DUAL;

CREATE TABLE Table2 (SEQ, REQ, RES) AS
  SELECT '001', 'X1', 'D' FROM DUAL UNION ALL
  SELECT '002', 'X1', 'D' FROM DUAL UNION ALL
  SELECT '003', 'X1', 'K' FROM DUAL UNION ALL
  SELECT '004', 'X1', 'C' FROM DUAL UNION ALL

  SELECT '001', 'X2', 'D' FROM DUAL UNION ALL
  SELECT '002', 'X2', 'D' FROM DUAL UNION ALL
  SELECT '003', 'X2', 'C' FROM DUAL UNION ALL

  SELECT '001', 'X3', 'D' FROM DUAL UNION ALL
  SELECT '002', 'X3', 'D' FROM DUAL UNION ALL
  SELECT '003', 'X3', 'C' FROM DUAL UNION ALL
  SELECT '004', 'X3', 'K' FROM DUAL UNION ALL

  SELECT '001', 'X4', 'D' FROM DUAL UNION ALL
  SELECT '002', 'X4', 'D' FROM DUAL UNION ALL
  SELECT '003', 'X4', 'C' FROM DUAL;

查询

SELECT SEQ, REQ
FROM   (
  SELECT t1.*,
         MAX( t1.SEQ ) OVER ( PARTITION BY t1.REQ ) AS max_seq,
         MAX( t2.RES ) KEEP ( DENSE_RANK LAST ORDER BY t1.SEQ )
                       OVER ( PARTITION BY t1.REQ ) AS max_res
  FROM   table1 t1
         LEFT OUTER JOIN table2 t2
         ON ( t1.seq = t2.seq AND t1.req = t2.req )
)
WHERE  SEQ = max_seq
AND    max_res = 'C'

输出

SEQ | REQ
:-- | :--
004 | X1 
003 | X2 

db <>提琴here

答案 2 :(得分:0)

这在Oracle SQL中可能略有不同

SELECT seq, req 
    FROM table1 
    WHERE seq in (SELECT max(seq) FROM table1) 
        AND seq NOT IN (SELECT seq FROM table2 WHERE res = 'C')