View previous topic :: View next topic
|
Author |
Message |
ovreddy
Active User
Joined: 06 Dec 2004 Posts: 211 Location: Keane Inc., Minneapolis USA.
|
|
|
|
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 |
|
|
priyesh.agrawal
Senior Member
Joined: 28 Mar 2005 Posts: 1448 Location: Chicago, IL
|
|
|
|
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 |
|
|
ovreddy
Active User
Joined: 06 Dec 2004 Posts: 211 Location: Keane Inc., Minneapolis USA.
|
|
|
|
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 |
|
|
Anoop Vipin
New User
Joined: 16 Aug 2007 Posts: 1 Location: Bangalore
|
|
|
|
i dont know abt that |
|
Back to top |
|
|
krishnasaikiah
New User
Joined: 14 Mar 2007 Posts: 24 Location: gurgaon
|
|
|
|
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 |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 632 Location: Wisconsin
|
|
|
|
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 |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 632 Location: Wisconsin
|
|
|
|
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 |
|
|
|