SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Want to categorize the salaries of employee.


Want to categorize the salaries of employee.

Author
Message
kiran.vaichalkar
kiran.vaichalkar
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 127
Hi Team,

I've Table People Where there are n no of employees involved.
The salaries of the employee are present in Column 'Salary'
So now, i wanted to count the no of Employees in the range of Salaries as below
1) >10000 & <30000
2) >30000 & <80000
3) >800000

Im trying the above logic as per the below code, can any one please help me understand this, why IM getting '0' for the counts even for no error in the query?


'Select Count(p2.SALARY)As Range1, Count(p3.SALARY)As Range2,Count(p4.SALARY)As Range3
From PEOPLE p1
JOIN PEOPLE p2
ON ((p1.pID = p2.pID) AND (p1.SALARY BETWEEN 10000 AND 20000))
JOIN PEOPLE p3
ON ((p1.pID = p3.pID) AND (p1.SALARY BETWEEN 20000 AND 80000))
JOIN PEOPLE p4
ON ((p1.pID = p4.pID) AND (p1.SALARY > 80000))'
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28107 Visits: 39939
i think you can streamline it a bit by using a neat technique using SUM(CASE..)

SELECT
SUM(CASE WHEN SALARY BETWEEN 10000 AND 20000 THEN 1 ELSE 0 END) As Range1,
SUM(CASE WHEN SALARY BETWEEN 20001 AND 80000 THEN 1 ELSE 0 END) As Range2,
SUM(CASE WHEN SALARY > 80000 THEN 1 ELSE 0 END) As Range3
From PEOPLE



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39213 Visits: 38529
kiran.vaichalkar (4/5/2013)
Hi Team,

I've Table People Where there are n no of employees involved.
The salaries of the employee are present in Column 'Salary'
So now, i wanted to count the no of Employees in the range of Salaries as below
1) >10000 & <30000
2) >30000 & <80000
3) >800000

Im trying the above logic as per the below code, can any one please help me understand this, why IM getting '0' for the counts even for no error in the query?


'Select Count(p2.SALARY)As Range1, Count(p3.SALARY)As Range2,Count(p4.SALARY)As Range3
From PEOPLE p1
JOIN PEOPLE p2
ON ((p1.pID = p2.pID) AND (p1.SALARY BETWEEN 10000 AND 20000))
JOIN PEOPLE p3
ON ((p1.pID = p3.pID) AND (p1.SALARY BETWEEN 20000 AND 80000))
JOIN PEOPLE p4
ON ((p1.pID = p4.pID) AND (p1.SALARY > 80000))'



Try this:



select
sum(case when p.Salary >= 10000 and p.Salary < 20000 then 1 else 0 end) as Range1,
sum(case when p.Salary >= 20000 and p.Salary < 80000 then 1 else 0 end) as Range2,
sum(case when p.Salary >= 80000 then 1 else 0 end) as Range3
from
People p




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Erin Ramsay
Erin Ramsay
SSC Eights!
SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)

Group: General Forum Members
Points: 817 Visits: 1099
You two are freaky scary sometimes.. BigGrin
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39213 Visits: 38529
Erin Ramsay (4/9/2013)
You two are freaky scary sometimes.. BigGrin


And why do you say that?? ;-)

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
pietlinden
pietlinden
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4855 Visits: 13166
It's because they know how to use the Force, and we don't...

Silly Us!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search