oraclesql如何在连接三个或更多表时删除由于多个左连接而形成的重复项?

a0zr77ik  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(444)

下面是我的表格结构,有三个表格a,b和x

Table a                                                       Table b 

STEP_ID   FORMAT  START_POSITION  FIELD_LENGTH  FUID                        FUID     FNAME
  1         null        4                 9        6                          6      A No
  1         null       14                10        7                          7      B No
  1         null       24                10        8                          8      C No
  1         null       36                 8        9                          9      D No

           Table x

     RID   IND    FLAG   CHAR  

      1     N       Y     3
      1     N       Y     1
      1     N       Y     0
      1     Y       N     2

下面是我试过的`

SELECT DISTINCT a.STEP_ID,
CASE
WHEN b.FNAME = 'A No' THEN 'A NO'
WHEN b.FNAME = 'B No' THEN 'B NO'
WHEN b.FNAME = 'C No' THEN 'C NO'
WHEN b.FNAME = 'D No' THEN 'D NO'
END AS F_ID,
a.FORMAT,
x.IND,
x.FLAG,
x.CHAR
(a.START_POSITION - lag(a.START_POSITION + a.FIELD_LENGTH,1,1) OVER (ORDER BY a.START_POSITION))
AS BLANK
FROM s1.a a
LEFT JOIN ( SELECT FNAME,FUID FROM s1.b) b ON b.FUID = a.FUID
LEFT JOIN ( SELECT RID,CHAR,IND,FLAG FROM s1.x) x ON x.RID = a.STEP_ID
WHERE  a.STEPID = 1;

我的输出应该是下面的格式,但是当我使用上面的查询时,我得到了重复的结果

STEP_ID    F_ID   FORMAT   IND   FLG   CHAR   BLANK   STATUS

  1        A NO    null     N     Y      3      3       S
  1        B NO    null     N     Y      1      1       S
  1        C NO    null     N     Y      0      0       S
  1        D NO    null     Y     N      2      2       S

如果所有行的char和blank中的值都相同,那么我需要状态为s,如果有不匹配的需要f,我已经为此工作了将近一个星期,并试图学习请帮助我。。。

koaltpgm

koaltpgm1#

当您将表x与表a连接起来时,它将给出16行的交叉相乘结果。另外,您不需要每次加入时都添加select语句。。
所以为了消除重复,你可以尝试使用行数函数。

Select * from (Select a.STEP_ID,
CASE
WHEN b.FNAME = 'A No' THEN 'A NO'
WHEN b.FNAME = 'B No' THEN 'B NO'
WHEN b.FNAME = 'C No' THEN 'C NO'
WHEN b.FNAME = 'D No' THEN 'D NO'
END AS F_ID,
a.FORMAT,
x.IND,
x.FLAG,
x.CHAR
(a.START_POSITION - lag(a.START_POSITION + a.FIELD_LENGTH,1,1) OVER (ORDER BY 
a.START_POSITION)) blank,
row_number() OVER (PARTITION BY FNAME order by FUID) rn 
FROM s1.a a LEFT JOIN si.b b ON ON b.FUID = a.FUID
             LEFT JOIN x ON x.RID = a.STEP_ID)
 WHERE rn = 1;

我也分享了一个链接,让你了解行\号。行\u编号
为了满足另外两个要求:可以联接表,其中一个使用rownum。见下表:

Select t.STEP_ID, t.FNAME, t.FORMAT,  x.RID, x.IND, x.FLAG, x.CHARS, t.blank, CASE WHEN t.blank = x.CHARS THEN 'S' ELSE 'F' END STATUS
from  
(Select a.*, rownum as global_id,  (a.START_POSITION - lag(a.START_POSITION + a.FIELD_LENGTH,1,1) OVER (ORDER BY a.START_POSITION)) blank, b.FNAME
from taba a
LEFT JOIN tabb b ON a.FUID = b.FUID
) t JOIN
(Select RID, IND, FLAG, CHARS, rownum rid2 from (Select tabx.*, row_number() OVER (PARTITION BY tabx.CHARS order by tabx.CHARS) rn2 from taba JOIN tabx ON STEP_ID = RID)  where rn2 = 1) x
ON t.STEP_ID = x.RID AND t.global_id = x.rid2;
+---------+-------+--------+-----+-----+------+-------+-------+--------+
| STEP_ID | FNAME | FORMAT | RID | IND | FLAG | CHARS | BLANK | STATUS |
+---------+-------+--------+-----+-----+------+-------+-------+--------+
| 1       | A No  | null   | 1   | N   | Y    | 0     | 3     | F      |
+---------+-------+--------+-----+-----+------+-------+-------+--------+
| 1       | B No  | null   | 1   | N   | Y    | 1     | 1     | S      |
+---------+-------+--------+-----+-----+------+-------+-------+--------+
| 1       | C No  | null   | 1   | Y   | N    | 2     | 0     | F      |
+---------+-------+--------+-----+-----+------+-------+-------+--------+
| 1       | D No  | null   | 1   | N   | Y    | 3     | 2     | F      |
+---------+-------+--------+-----+-----+------+-------+-------+--------+

另一种方法是使用非常简单的空白列,这样您的状态将是's',并且与上面的输出顺序相同:

Select t.*, x.RID, x.IND, x.FLAG, x.CHARS, CASE WHEN t.blank = x.CHARS THEN 'S' ELSE 'F' END STATUS from (Select a.STEP_ID, b.FNAME,a.FORMAT,  (a.START_POSITION - lag(a.START_POSITION + a.FIELD_LENGTH,1,1) OVER (ORDER BY a.START_POSITION)) blank from taba a
LEFT JOIN tabb b ON a.FUID = b.FUID) t
LEFT JOIN tabx x ON t.STEP_ID = x.RID and t.BLANK = x.CHARS
+---------+-------+--------+-------+-----+-----+------+-------+--------+
| STEP_ID | FNAME | FORMAT | BLANK | RID | IND | FLAG | CHARS | STATUS |
+---------+-------+--------+-------+-----+-----+------+-------+--------+
| 1       | A No  | null   | 3     | 1   | N   | Y    | 3     | S      |
+---------+-------+--------+-------+-----+-----+------+-------+--------+
| 1       | B No  | null   | 1     | 1   | N   | Y    | 1     | S      |
+---------+-------+--------+-------+-----+-----+------+-------+--------+
| 1       | C No  | null   | 0     | 1   | N   | Y    | 0     | S      |
+---------+-------+--------+-------+-----+-----+------+-------+--------+
| 1       | D No  | null   | 2     | 1   | Y   | N    | 2     | S      |
+---------+-------+--------+-------+-----+-----+------+-------+--------+

相关问题