简体   繁体   中英

Sub query using Oracle 8i

We have a SQL script that creates a CSV using the following:

SELECT DISTINCT auf_kopf.auf_nr, TO_CHAR(auf_kopf.erfass_dat,'DD/MM/YYYY'), TO_CHAR(auf_kopf.liefer_dat,'DD/MM/YYYY'), v_ord_custname.cust_name, auf_pos.auf_pos, v_auf_stat_0.anz, v_reason_code_1.reason_code, v_reason_code_1.reason_desc, v_auf_pos_vmat.glas1, auf_pos.glas2, auf_kopf.auf_off, auf_pos.ver_art
FROM auf_kopf, auf_pos, v_auf_pos_vmat, v_auf_stat_0, v_ord_custname, v_reason_code_1, glas_daten_basis
WHERE auf_kopf.erfass_dat >sysdate-7
AND auf_kopf.kopf_reason IS NOT NULL
AND (auf_kopf.auf_nr=auf_pos.auf_nr(+))
AND (auf_pos.auf_nr=v_auf_pos_vmat.auf_nr(+))
AND (auf_pos.auf_pos=v_auf_pos_vmat.auf_pos(+))
AND (auf_pos.variante=v_auf_pos_vmat.variante(+))
AND (auf_pos.auf_nr=v_auf_stat_0.auf_nr)
AND (auf_pos.auf_pos=v_auf_stat_0.auf_pos)
AND (auf_pos.variante=v_auf_stat_0.variante)
AND (auf_kopf.auf_nr=v_ord_custname.order_no)
AND (auf_kopf.auf_nr=v_reason_code_1.order_no(+));

There is a table called glas_daten_basis that contains descriptions for the codes shown from v_auf_pos_vmat.glas1 and auf_pos.glas2 . How do we show the description also as I think a sub query is required but I couldn't get it to work.

We need glas_daten_basis.gl_bez (description) for v_auf_pos_vmat.glas1 and auf_pos.glas2 . The codes can be matched using glas_daten_basis.idnr and v_auf_pos_vmat.glas1 .

As far as I can follow this, I think you just need to join to glas_daten_basis twice:

SELECT DISTINCT auf_kopf.auf_nr, TO_CHAR(auf_kopf.erfass_dat,'DD/MM/YYYY'), 
  TO_CHAR(auf_kopf.liefer_dat,'DD/MM/YYYY'), v_ord_custname.cust_name, 
  auf_pos.auf_pos, v_auf_stat_0.anz, v_reason_code_1.reason_code,
  v_reason_code_1.reason_desc,
  v_auf_pos_vmat.glas1 ||' '|| gd_basis1.gl_bez as glas1,
  auf_pos.glas2 ||' '|| gd_basis2.gl_bez as glas2,
  auf_kopf.auf_off, auf_pos.ver_art
FROM auf_kopf, auf_pos, v_auf_pos_vmat, v_auf_stat_0, v_ord_custname,
  v_reason_code_1, glas_daten_basis gd_basis1, glas_daten_basis gd_basis2
WHERE auf_kopf.erfass_dat >sysdate-7
AND auf_kopf.kopf_reason IS NOT NULL
AND (auf_kopf.auf_nr=auf_pos.auf_nr(+))
AND (auf_pos.auf_nr=v_auf_pos_vmat.auf_nr(+))
AND (auf_pos.auf_pos=v_auf_pos_vmat.auf_pos(+))
AND (auf_pos.variante=v_auf_pos_vmat.variante(+))
AND (auf_pos.auf_nr=v_auf_stat_0.auf_nr)
AND (auf_pos.auf_pos=v_auf_stat_0.auf_pos)
AND (auf_pos.variante=v_auf_stat_0.variante)
AND (auf_kopf.auf_nr=v_ord_custname.order_no)
AND (auf_kopf.auf_nr=v_reason_code_1.order_no(+))
AND (v_auf_pos_vmat.glas1 = gd_basis1.idnr(+))
AND (auf_pos.glas2 = gd_basis2.idnr(+));

I've aliased the two references to that table so you can distinguish between them. One instance of the table is outer joined to v_auf_pos_vmat , the other to auf_pos . It's been a while since I've used the old (+) notation, but I think I've got them in the right place for what you're doing...

Incidentally, the brackets around all the conditions are unnecessary though, since there is no ambiguity to resolve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM