Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist 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 Create procedure issues -628 when add... chandraBE DB2 1 Mon Sep 18, 2017 12:16 pm
No new posts Can we limit length in concatenation ... balaji81_k DB2 7 Tue Aug 22, 2017 2:50 am
No new posts Command to find TMEMBER name for a TPIPE Siva NKK Kothamasu IMS DB/DC 0 Wed Aug 16, 2017 6:08 pm
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts find RACF group for access to spooled... jzhardy JCL & VSAM 1 Mon May 08, 2017 11:46 am

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us