Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

SQL query to find out the maximum no.of Bs.

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
sunnybunny

New User


Joined: 29 Nov 2005
Posts: 49

PostPosted: Fri Jul 27, 2007 9:53 am    Post subject: SQL query to find out the maximum no.of Bs.
Reply with quote

Hello Friends,

I want to execute a query with following requirement.

I have a table with two fields A and B. The value of A field can occur morethan once in B field.

For ex if the values A and B are as follows.

Code:
A         B
100       100
          100
          100
200       200
          200
300       300
          300
          300
          300


Now I want to find out the maximum no.of Bs. That means in above example 300 comes as 4 times. So I want that number.

Could you please provide me the SQL query for that.

Regards,
Suneel.
Back to top
View user's profile Send private message

kussu
Warnings : 1

New User


Joined: 16 May 2007
Posts: 33
Location: India

PostPosted: Fri Jul 27, 2007 7:34 pm    Post subject: Re: SQL query to find out the maximum no.of Bs.
Reply with quote

Let the table name be Alpha

Select count(b)
From alpha
Where b= 100
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Fri Jul 27, 2007 8:33 pm    Post subject:
Reply with quote

Hello Pyari,

Please re-read the requirement.

Your suggestion will count the number of colB rows with a value of 100. That is not the requirement.

The requirement is to identify which value in colB is present in the most rows.


Suneel - please post sample data where colA and colB do not always have the same value or explain why there would be real data like the rows you defined.
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Fri Jul 27, 2007 8:40 pm    Post subject:
Reply with quote

kussu's solution will only count the number or 100's that exist and would require you to execute for each possible value in Column B then pick the highest count.

Code:

SELECT MAX(TT.CountVal)
FROM (SELECT B, SUM(1) as CountVal
      FROM table
      GROUP BY B) TT


Will get you the highest count, but it won't tell you which one has that count. Do you need to know which one has that highest count also?

Code:

SELECT TT.A, TT.B FROM
(select B, count(B)
from table
group by A) TT
WHERE TT.B = (SELECT MAX(TT2.B) FROM (select A, count(B)
from table
group by A)TT2)


This also works

Code:


SELECT B, Count(B)
FROM table
GROUP BY B
HAVING COUNT(B) = (SELECT MAX(TT.C) FROM (select count(B) as C
FROM table
GROUP BY B) TT)
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts find particular member name in PDS us... ravi243 CLIST & REXX 10 Mon Dec 19, 2016 6:44 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us