View previous topic :: View next topic |
Author |
Message |
Catherine Beginner
Joined: 30 Aug 2006 Posts: 32 Topics: 15 Location: Chennai
|
Posted: Tue Mar 27, 2007 5:18 am Post subject: Group by on 3 Columns and get unique rows query |
|
|
Hi,
I have got 2 tables
Table Emp: ID Primary key
Code: |
ID NAME
1 A
2 B
3 C
4 D
|
Table TRANSACTION
Code: |
ID TRNS DATE
1 CR 12
1 DB 12
2 CR 13
3 CR 14
3 DB 14
4 CR 15
|
Now i execute this query
Code: |
SELECT A.ID, A.NAME, B.TRNS
FROM EMP A, TRANSACTION B
WHERE A.ID = B.ID
GROUP BY A.ID, A.NAME, B.TRNS
HAVING COUNT(B.TRNS_TY) = 1
|
I expect my query to return only 2 rows
But i donot get this result
Can you explain me how this Query executes! |
|
Back to top |
|
|
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Tue Mar 27, 2007 5:25 am Post subject: |
|
|
1) What is TRNS_TY which is not shown on your table
2) What is the result that you ARE getting? _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
|
Catherine Beginner
Joined: 30 Aug 2006 Posts: 32 Topics: 15 Location: Chennai
|
Posted: Tue Mar 27, 2007 6:51 am Post subject: SQL Query |
|
|
Nic,
Sorry thats B.TRNS instead of B.TRNS_TY
SELECT A.ID, A.NAME, B.TRNS
FROM EMP A, TRANSACTION B
WHERE A.ID = B.ID
GROUP BY A.ID, A.NAME, B.TRNS
HAVING COUNT(B.TRNS) = 1
Result
1 A CR
1 A DB
2 B CR
3 C CR
3 C DB
4 D CR |
|
Back to top |
|
|
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Tue Mar 27, 2007 6:57 am Post subject: |
|
|
well - I can see that b.trns has only 1 'cr' and/or only 1 'dr' for each id so count will always be 1 - try count(b.id) = 1 _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
|
Catherine Beginner
Joined: 30 Aug 2006 Posts: 32 Topics: 15 Location: Chennai
|
Posted: Tue Mar 27, 2007 7:24 am Post subject: SQL Query |
|
|
Nic,
I get the same result |
|
Back to top |
|
|
vkphani Intermediate
Joined: 05 Sep 2003 Posts: 483 Topics: 48
|
Posted: Tue Mar 27, 2007 7:33 am Post subject: |
|
|
Try this untested SQL:
Code: |
SELECT DISTINCT A.ID, A.NAME, B.TRNS
FROM EMP A, TRANSACTION B
WHERE A.ID IN (SELECT ID
FROM [TRANSACTION]
GROUP BY ID
HAVING COUNT(ID) = 1);
|
|
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Tue Mar 27, 2007 7:42 am Post subject: |
|
|
catherine,
The reason your sql did not work is that you are grouping by 3 columns (id,name & trns) By grouping like that you will have more unique rows. If your intention is to get single rows from transaction table then you need the group by on just the ID column.
Try this
Code: |
SELECT A.ID
,A.NAME
,B.TRNS
FROM EMP A
,TRANSACTION B
WHERE A.ID = B.ID
AND A.ID IN (SELECT ID
FROM TRANSACTION
GROUP BY ID HAVING COUNT(*) = 1)
;
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Tue Mar 27, 2007 7:43 am Post subject: |
|
|
vkphani wrote: | Try this untested SQL:
Code: |
SELECT DISTINCT A.ID, A.NAME, B.TRNS
FROM EMP A, TRANSACTION B
WHERE A.ID IN (SELECT ID
FROM [TRANSACTION]
GROUP BY ID
HAVING COUNT(ID) = 1);
|
|
Vkphani,
It will NOT work
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
Catherine Beginner
Joined: 30 Aug 2006 Posts: 32 Topics: 15 Location: Chennai
|
Posted: Tue Mar 27, 2007 8:52 am Post subject: SQL Query |
|
|
Kolusu,
I just want to confirm this
The output of this Query
SELECT A.ID, A.NAME, B.TRNS
FROM EMP A, TRANSACTION B
WHERE A.ID = B.ID
GROUP BY A.ID, A.NAME, B.TRNS
will be
1 A CR
1 A DB
2 B CR
3 C CR
3 C DB
4 D CR
When i add a a Having clause
HAVING COUNT(TRNS) = 1, it gives all the records as above because
TRNS has 2 unique values CR & DB
But if i give HAVING COUNT(ID) = 1,
SELECT A.ID, A.NAME, B.TRNS
FROM EMP A, TRANSACTION B
WHERE A.ID = B.ID
GROUP BY A.ID, A.NAME, B.TRNS
HAVING COUNT(ID) = 1
I should have got the rows where ID IS UNIQUE ie
2 B CR
4 D CR
Does the DB2 consider the TRNS in the Group by clause, even though i give only count(ID)? |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Tue Mar 27, 2007 9:29 am Post subject: |
|
|
Catherine,
The HAVING clause is derived from the GROUP by clause, so even though you mentioned the COUNT(ID) it is going to check based on the Group by clause
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
|
|