MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Group by on 3 Columns and get unique rows query

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
Catherine
Beginner


Joined: 30 Aug 2006
Posts: 32
Topics: 15
Location: Chennai

PostPosted: Tue Mar 27, 2007 5:18 am    Post subject: Group by on 3 Columns and get unique rows query Reply with quote

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
Code:

2 B  CR
4 D  CR

But i donot get this result
Can you explain me how this Query executes!
Back to top
View user's profile Send private message
Nic Clouston
Advanced


Joined: 01 Feb 2007
Posts: 1075
Topics: 7
Location: At Home

PostPosted: Tue Mar 27, 2007 5:25 am    Post subject: Reply with quote

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
View user's profile Send private message
Catherine
Beginner


Joined: 30 Aug 2006
Posts: 32
Topics: 15
Location: Chennai

PostPosted: Tue Mar 27, 2007 6:51 am    Post subject: SQL Query Reply with quote

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
View user's profile Send private message
Nic Clouston
Advanced


Joined: 01 Feb 2007
Posts: 1075
Topics: 7
Location: At Home

PostPosted: Tue Mar 27, 2007 6:57 am    Post subject: Reply with quote

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
View user's profile Send private message
Catherine
Beginner


Joined: 30 Aug 2006
Posts: 32
Topics: 15
Location: Chennai

PostPosted: Tue Mar 27, 2007 7:24 am    Post subject: SQL Query Reply with quote

Nic,

I get the same result
Back to top
View user's profile Send private message
vkphani
Intermediate


Joined: 05 Sep 2003
Posts: 483
Topics: 48

PostPosted: Tue Mar 27, 2007 7:33 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Tue Mar 27, 2007 7:42 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Tue Mar 27, 2007 7:43 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Catherine
Beginner


Joined: 30 Aug 2006
Posts: 32
Topics: 15
Location: Chennai

PostPosted: Tue Mar 27, 2007 8:52 am    Post subject: SQL Query Reply with quote

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
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Tue Mar 27, 2007 9:29 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group