IBM MAINFRAME HELP & SUPPORT FORUMS
Technical Forums for IBM Mainframe Applications like COBOL, JCL, CICS, DB2, FileAid, DFSORT, Endevor, Xpediter, CoolGen, CA-7&11, AbendAid, IMS, IDMS, PL/I, MqSeries, SyncSort, Assembler, ChangeMan, Easytrieve, InterTest, REXX, CLIST etc...
 

Select Querry to display no of records

THIS IS AN ARCHIVE FORUM: CLICK HERE TO GO TO THE ORIGINAL TOPIC

 
       IBMMAINFRAMES.com - IBM Mainframe Support Forums Index -> DB2
View previous topic :: View next topic  
Author Message
siva102



Joined: 28 Nov 2007
Posts: 40
Location: Chennai

Posted: Wed Aug 20, 2008 12:56 pm    Post subject: Select Querry to display no of records  

Hi all,

I am having a table which contains 13 records. Let the table name is Employ and the column is name.

My need is to display all the names along with the total count of names.

select * from employ with ur; --> Displays all the names.

Select count(*) from employ with ur ; --> total count

How to mix thease querry so that i can get the o/p ike below,


Name Count

sa 13
as
etc.....


can anybody please help me out from this prob.
Back to top  
Srihari Gonugunta



Joined: 14 Sep 2007
Posts: 184
Location: Pune

Posted: Wed Aug 20, 2008 1:07 pm    Post subject:  

Select name,count(*) from employ
group by name
Back to top  
siva102



Joined: 28 Nov 2007
Posts: 40
Location: Chennai

Posted: Wed Aug 20, 2008 3:44 pm    Post subject: Reply to: Select Querry to display no of records  

hi Srihari,

Thanks for replying.

But in the query that u have provided i am not getting the total count of number of rows instead getting how many times the name is there in the table.


For e.g - >

Let the table contains 5 values. like below,

sanjeet
sandeep
sam
sarmi
sapnaa

By running the querry i am getting

Sanjeet - 1
sandeep - 1
like wise.....

But i need my o/p to display all the 5 names along with count = 5

I suppose u got my question. If you need anything else please let me know.
Back to top  
Srihari Gonugunta



Joined: 14 Sep 2007
Posts: 184
Location: Pune

Posted: Wed Aug 20, 2008 3:58 pm    Post subject:  

seems to be bit strange requirement. You can try the following query

SELECT Name, (Select count(*) from employ) as Count
from employ
Back to top  
siva102



Joined: 28 Nov 2007
Posts: 40
Location: Chennai

Posted: Wed Aug 20, 2008 4:23 pm    Post subject: Reply to: Select Querry to display no of records  

Hi,

Yah it is little bit strange only.

But this time it got corrected 50%. The o/p is coming like the following,

Code: Name       count
sanjeet                5
sandeep               5
sam                     5
sarmi                   5
sapnaa                 5


But i need the count to be displayed once only.

Means the o/p should look like the below,
Code:
Name                count
sanjeet                   5
sandeep                   
sam                       
sarmi                       
sapnaa                     



Thanks for all your answers.

Could you please help me regarding this issue.
Back to top  
jegan_selvan



Joined: 23 Oct 2006
Posts: 1

Posted: Wed Aug 20, 2008 6:16 pm    Post subject:  

can you try this..

select name, count(name) from employ
group by name

Regards,

Jegan S
Back to top  
siva102



Joined: 28 Nov 2007
Posts: 40
Location: Chennai

Posted: Wed Aug 20, 2008 6:30 pm    Post subject: Reply to: Select Querry to display no of records  

Hi,

Thanks for ur reply.

I have already tried that but i am not getting my required o/p.

Here the o/p is like below,
Code:
Name       count
sapnaa       1
sarmi          1
sam            1
sandeep      1
sanjeet       1

I still not able to get my answer.
Please anybody who know the ans let me know.
Back to top  
DB2Buddha



Joined: 20 Aug 2008
Posts: 2
Location: Düsseldorf

Posted: Thu Aug 21, 2008 1:26 pm    Post subject:  

Hello Siva.

Try this

SELECT MIN(Name) AS Names, CHAR(COUNT(*)) AS Amount
FROM Employ
UNION
SELECT Name as Names, CHAR(' ') AS Amount
FROM employ
WHERE Name <> (SELECT MIN(Name) FROM Employ)
ORDER BY 1
WITH UR

But be careful. I think, the performance of this query is rather bad

Ciao, DB2Buddha
Back to top  
dick scherrer



Joined: 23 Nov 2006
Posts: 8733
Location: 221 B Baker St

Posted: Thu Aug 21, 2008 8:27 pm    Post subject:  

Hello and welcome to the forums,

Did you test this query to make sure it provides the desired result?

Please do not post untested queries (regardless of your suspicion about performance).
Back to top  
DB2Buddha



Joined: 20 Aug 2008
Posts: 2
Location: Düsseldorf

Posted: Fri Aug 22, 2008 1:17 pm    Post subject: Reply to: Select Querry to display no of records  

Hi.

I reviewed the statement. It works under the condition, that the values of the column "name" are unique. Sorry, i did not mention that. :oops:

Otherwise the statement delivers a false result. If the column "name" is not unique, even a "union all" would not deliver the desired result.

Regards, DB2Buddha
Back to top  
dbzTHEdinosauer



Joined: 20 Oct 2006
Posts: 1639
Location: germany

Posted: Fri Aug 22, 2008 2:57 pm    Post subject: Reply to: Select Querry to display no of records  

siva102,

why generate an illogical, ambigous report?

Code:
Name                count
sanjeet                   5
sandeep                   
sam                       
sarmi                       
sapnaa


why not?

Code:
Name                count
sanjeet                   1
sandeep                   1
sam                       1
sarmi                     1 
sapnaa                    1
Total Names               5


Code:
Select name
     , count(*)
from employ
group by name
Union
Select 'Total Names'
     , count(*)
from employ
Back to top  
dick scherrer



Joined: 23 Nov 2006
Posts: 8733
Location: 221 B Baker St

Posted: Sat Aug 23, 2008 3:16 am    Post subject: Reply to: Select Querry to display no of records  

Hello,

Quote: I reviewed the statement. It works under the condition, that the values of the column "name" are unique. . . .Sorry, i did not mention that.
Not to worry (someone is often watching. . .).

Thank you for the follow up :)

d
Back to top  
 
       IBMMAINFRAMES.com - IBM Mainframe Support Forums Index -> DB2
Page 1 of 1
THIS IS AN ARCIVE FORUM IN READ ONLY MODE. IF YOU WANT TO ASK YOUR DOUBTS USE THE ACTUAL FORUM