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 Needed for the following problem

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: SQL Query Needed for the following problem
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: 1452
Location: Chicago, IL

PostPosted: Fri Oct 28, 2005 3:30 pm    Post subject: Re: SQL Query Needed Very Urgent!!!
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    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
This topic is locked: you cannot edit posts or make replies. SORT trick needed bshkris SYNCSORT 6 Tue May 02, 2017 4:35 am
No new posts Strings with double quotes having pro... raja Arumugam All Other Mainframe Topics 11 Thu Mar 30, 2017 10:34 am
No new posts Problem reading GTF trace output danik56 ABENDS & Debugging 7 Thu Mar 16, 2017 1:02 pm
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm


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