IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

DB2 Query to get the count of Unique selection


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Chidane

New User


Joined: 25 Nov 2021
Posts: 34
Location: India

PostPosted: Fri Feb 13, 2026 10:45 pm
Reply with quote

Hi Team,

I have the requirement as follows. I have a DB2 table with following fields

Code:
FUNC       NAME       LOCATION
ABC        JAMES      001
ABC        JAMES      002
ABC        JAMES      003
DEF        JAMES      001
DEF        JAMES      002
DEF        TIM        001
ABC        ROY        001
DEF        ROY        002
XYZ        ROY        001


Here I would like to know, for each FUNC, how many unique NAME are tagged. So I would like to have output as

Code:
FUNC      COUNT
ABC          2
DEF          3
XYZ           1


Here Func ABC has 2 persons tagged (James and Roy) ; DEF has 3 persons tagged (James, Tim and Roy) ; XYZ has only ROY tagged.
I have tried putting the code as , however this is not giving correct results
SELECT FUNC, COUNT(NAME) FROM TABLE_A
GROUP BY FUNC

Can you please help me to acheive this result
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2283
Location: USA

PostPosted: Fri Feb 13, 2026 11:01 pm
Reply with quote

RTFM about SQL option: GROUP BY

P.S.
Please, do not post topics in this style:
Quote:
I'm given the task to do this and this. I have no idea, how to do it. Can you please do my job for me?
Thank you very much.
Back to top
View user's profile Send private message
Chidane

New User


Joined: 25 Nov 2021
Posts: 34
Location: India

PostPosted: Fri Feb 13, 2026 11:04 pm
Reply with quote

Thanks.
I tried this and worked.

SELECT FUNC, COUNT(DISTINCT NAME) FROM TABLE_A
GROUP BY FUNC
Back to top
View user's profile Send private message
View previous topic : : View next topic  
Post new topic   Reply to topic All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Need help with ADABAS query (COBOL-AD... All Other Mainframe Topics 1
No new posts combine multiple unique records into ... DFSORT/ICETOOL 2
No new posts CA7 count with DATE variable CA Products 0
No new posts To get the count of rows for every 1 ... DB2 3
No new posts To find whether record count are true... DFSORT/ICETOOL 6
Search our Forums:


Back to Top