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

SQL Query Needed for the following problem


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

Active User


Joined: 06 Dec 2004
Posts: 211
Location: Keane Inc., Minneapolis USA.

PostPosted: Fri Oct 28, 2005 2:33 pm
Reply with quote

Hi All,

Please provide me a SINGLE Query for the following.

In a Table called EMP I have salaries ranging from 1000 to 20000. I want to know the count by salary range. My Query should give the following output..

Sal Range No Employee
-------------------------------
1000 - 5000 3
5000-10000 4
10000-15000 0
15000-20000 8


Strictly I need it in single query not in a Program or Cursor. I dont have an authority to create a table for ranges. So Ranges should be created dynamically with 5000 difference.

Immediate reply appreciated.

Thanks,
Reddy.
Back to top
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1448
Location: Chicago, IL

PostPosted: Fri Oct 28, 2005 3:30 pm
Reply with quote

Hi Reddy,

Nice to see you posted queries on SQL....

Code:
SELECT COUNT(EMP-NAME) FROM TAB WHERE SALARY BETWEEN '1000' and '5000'   
UNION                         
SELECT COUNT(EMP-NAME) FROM TAB WHERE SALARY BETWEEN '5000' and '10000'   
UNION
SELECT COUNT(EMP-NAME) FROM TAB WHERE SALARY BETWEEN '10000' and '15000'   
UNION
SELECT COUNT(EMP-NAME) FROM TAB WHERE SALARY BETWEEN '15000' and '20000';


Please try & let me know, whether it works for you....

Regards,

Priyesh.
Back to top
View user's profile Send private message
ovreddy

Active User


Joined: 06 Dec 2004
Posts: 211
Location: Keane Inc., Minneapolis USA.

PostPosted: Sat Oct 29, 2005 2:42 pm
Reply with quote

Hi Priyesh,

Thanks for your reply. In my Table the salaries may vary in a variety of manner. In such a case I dont want to write that many SQL Statements. I need the RANGE should be generated dynamically.

Thanks,
Reddy
Back to top
View user's profile Send private message
Anoop Vipin

New User


Joined: 16 Aug 2007
Posts: 1
Location: Bangalore

PostPosted: Tue Oct 09, 2007 2:19 pm
Reply with quote

i dont know abt that
Back to top
View user's profile Send private message
krishnasaikiah

New User


Joined: 14 Mar 2007
Posts: 24
Location: gurgaon

PostPosted: Tue Oct 09, 2007 3:43 pm
Reply with quote

Hi please suggest me a combined query for the following two queries:-

Select column A,
column B
into :dcltablec.columnA
and :dcltablec.columnB
from table C
where emp id = :ws-emp-id
and emp-nbr = :ws-emp-nbr
and emp-name = :ws-emp-name

Delete table D
where column A = :dcltablec.columnA
and column B = :dcltablec.columnB
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Tue Oct 09, 2007 5:54 pm
Reply with quote

priyesh.agrawal:

Aren't UNION clauses heavy hitters on the DB? Also your statement will double count all the boundary conditions. Someone who makes exactly 5000 will end up in both the first and second queries because BETWEEN is inclusive.

If you do something like this then you don't double count your boundaries.

Code:

SELECT SUM(1) , SRange =   
       Case
            When Salary > 1000 and salary <= 5000 Then '1000 - 5000'
            When Salary > 5000 and salary <= 10000 Then '5000 - 10000'
            When Salary > 10000 and salary <= 15000 Then '10000 - 15000'
            When Salary > 15000 and salary <= 20000 Then '15000 - 20000'
       End
FROM Table
Group By SRange


You could subsitute :WS-Low and :WS-High in place of the 1000, 5000, etc
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Tue Oct 09, 2007 6:04 pm
Reply with quote

Automatically figuring out the ranges will be very difficult maybe not even possible with a single query. What is the high end of the salary 1000000, 1005000? Without knowing that, your query is infinite even though you may get counts of 0, what is the limit? It is a poorly designed requirement to do this without a program.

You need some well planned math. First you need to find the maximum and minimum values in the SALARY column, then you need to do some logic on that to find your ranges of 5000. ROUNDUP((MaxVal - MinVal)/5000) = the number of ranges you would have. Then find out the value of MinVal rounded down to the nearest 5000 and start generating your range values up to the MaxVal rounded up to the next 5000. Then you can generate your query based on those range values.

I honestly don't see how this can be done with even a single complex SQL statement. Even if it could, the maintainability of it would be a nightmare. You may be able to do it with a store procedure because those can encapsulate logic.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Map Vols and Problem Dataset All Other Mainframe Topics 2
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
Search our Forums:

Back to Top