简体   繁体   中英

Select Records With a Count of greater than 1 Where a field has multiple values

I'm having an issue trying to select records where there are different values for the field (invH.CODE) which is a VARCHAR type. For example, if there are 2 different codes with values such as "PLCH" and "ABCD". Below is the sql I have so far:

SELECT invH.INVOICE_NO
FROM SCHEMA.INVOICE_H invH
WHERE invH.STATUS = 'X'
GROUP BY invH.INVOICE_NO
HAVING COUNT (DISTINCT invH.CODE) > 1

I'm trying to select records where there is an instance in which two different values for that field are present (there can be multiple values for this field in one record). I'm unsure of how to get that syntactically. Please let me know of any efficient way of getting this.

I'm using DB2/AIX64 Version 9.5.3.

I think , if you use the self joins between the table to get the values , otherwise you need to use subquery to solve it . In case of join I think below query should work

SELECT DISTINCT invH1.INVOICE_NO
FROM SCHEMA.INVOICE_H invH1 , SCHEMA.INVOICE_H invH2
WHERE invH1.STATUS = invH2.STATUS
and invH1.INVOICE_NO = invH2.INVOICE_NO
and invH1.STATUS = 'X'
and invH1.CODE <> invH2.CODE

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