简体   繁体   中英

Ambiguous column name 'DELETED' when filtering

Good morning.

Without saying to much, I am joining two tables where each contain a row with the same name field DELETED. I am able to join both tables and get results, the problem is when I try to filter out those orders that are DELETED "X" and not DELETED "NULL", I get the ambiguous column name.

SELECT

OCRI.DDORD#,
OCRH.DCBCUS,
OCRI.DELETED

from ocri
join OCRH on DCORD# = DDORD#    
where OCRI.dditst <> 'C'
and DELETED <> 'X'

I have tried and OCRI.DELETED <> 'X' but then I get no results.

I would like to be able to filter out the X

DDORD#  DCBCUS     DELETED
194991  150482      NULL
195000  263609      X
195381  263609      X
195387  246045      NULL
195724  146551      NULL

Qualify ALL column references in your query. You haven't provided enough information to know which columns belong where.

Then, you need to handle NULL values.

An example:

SELECT rh.DDORD#, ri.DCBCUS, ri.DELETED
FROM ocri ri JOIN
     OCRH rh
     ON ri.DCORD# = rh.DDORD#    
WHERE rh.rhdditst <> 'C' AND
      (ri.DELETED <> 'X' OR ri.DELETED IS NULL);

Of course, I don't know what the tables look like so the qualifications are probably wrong.

Note that standard SQL has a NULL -safe comparison, which is supported by some databases. This looks like:

WHERE rh.rhdditst <> 'C' AND
      ri.DELETED IS DISTINCT FROM 'X';

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