All, Imagine you have a database (File: INPUTPF) that looks as such...
Rec No | Client_Name | Class | Fld1 | Fld2 |
---|---|---|---|---|
1 | Abbey | 123 | Y | |
2 | Abbey | 999 | Y | |
3 | Abbey | 234 | Y | |
4 | Bill | 222 | N | |
5 | Bill | 333 | Y | N |
6 | Bill | 444 | Y | N |
7 | Mike | 987 | Y | N |
8 | Mike | 000 | Y | N |
9 | Mike | 999 | ||
10 | Tom | 666 | Y | N |
11 | Tom | 777 | Y | N |
12 | Tom | 999 | Y |
... and in a prior SQL statement I wrote the following code to capture ALL records where ALL of the clients if Fld1 were different:
{DROP TABLE IF EXISTS OUTPUT1;
CREATE TABLE LIB1/OUTPUT1 AS(
SELECT *
FROM INPUTPF
WHERE Client_Name IN (
SELECT Client_Name
FROM INPUTPF
GROUP BY Client_Name
HAVING COUNT(DISTINCT Fld1) > 1)
)
WITH DATA;}
This SQL works perfect and writes out to the table OUTPUT1 ALL records for Bill (Rec No 4-6) and Mike (Rec No 7 - 9) as Fld1 for both Bill & Mike are NOT identical. That is, Bill Fld1 is set to 'Y' for records 5 & 6 bot not record 4. Mike has Fld1 set to 'Y' for records 7 & 8 but not for record 9. The OUPPUT1 table looks as follows-
Rec No | Client_Name | Class | Fld1 | Fld2 |
---|---|---|---|---|
4 | Bill | 222 | N | |
5 | Bill | 333 | Y | N |
6 | Bill | 444 | Y | N |
7 | Mike | 987 | Y | N |
8 | Mike | 000 | Y | N |
9 | Mike | 999 |
So Far so good!
However, now I need a new SQL that will test FLD2 for the same OUTPUT1 table but only add records if they were NOT written during the FLD1 test. So my OUTPUT1 file would look the following -
Rec No | Client_Name | Class | Fld1 | Fld2 |
---|---|---|---|---|
4 | Bill | 222 | N | |
5 | Bill | 333 | Y | N |
6 | Bill | 444 | Y | N |
7 | Mike | 987 | Y | N |
8 | Mike | 000 | Y | N |
9 | Mike | 999 | ||
10 | Tom | 666 | Y | N |
11 | Tom | 777 | Y | N |
12 | Tom | 999 | Y |
Now the OUPUT1 file has Tom added as there are differences in Fld2 (Rec 10 & 11 are 'Y' but rec 12 is blank). The problem I face is with Mike. Rec No 9 for Mike differs in both Fld1 & Fld2 but I only want is written to the OUTPUT1 file one time and not twice (i.e. once for the FLD1 pass and again for the FLD2 pass). That is to say if a client's FLD1 is written to the OUTPUT1 file AND FLD2 is different DO NOT write it out again.
So my question is this-
What would be the SQL code that allow me group Client_Name and write out all of the records for Client_Name where FLD1 is NOT the same(i.e. prior code above) AND add to OUTPUT1 when Client FLD2 is different and is not added to OUTPUT1 file yet because FLD1 differences. Simply said I'm trying to avoid the addition of Mike to the table twice as he has different FLD1 AND FLD2 records.
As always thanks in advance for your time and help!
An OR is the answer
Change your line
HAVING COUNT(DISTINCT Fld1) > 1)
To
HAVING ( COUNT(DISTINCT Fld1) > 1 or COUNT(DISTINCT Fld2) > 1 ))
so the full select is
{DROP TABLE IF EXISTS OUTPUT1;
CREATE TABLE LIB1/OUTPUT1 AS(
SELECT *
FROM INPUTPF
WHERE Client_Name IN (
SELECT Client_Name
FROM INPUTPF
GROUP BY Client_Name
HAVING ( COUNT(DISTINCT Fld1) > 1 or COUNT(DISTINCT Fld2) > 1 ))
)
WITH DATA;}
Thanks
Oops I had not seen @JNevill comment above which is the same. I might delete this answer.