简体   繁体   English

选择计数大于1的记录,其中一个字段具有多个值

[英]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. 我在尝试选择具有VARCHAR类型的字段(invH.CODE)具有不同值的记录时遇到问题。 For example, if there are 2 different codes with values such as "PLCH" and "ABCD". 例如,如果有2个不同的代码,例如“ PLCH”和“ ABCD”。 Below is the sql I have so far: 下面是我到目前为止的SQL:

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. 我正在使用DB2 / AIX64版本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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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