Home All Groups Group Topic Archive Search About
Author
18 May 2006 9:35 PM
rodchar
hey all,

what's the best way to express this in a query?

for each employee
take the salary and determine which range the particular salary falls in.
for instance:
40k
falls between 35-40k so the category is 1

number of categories are 1-12

thanks,
rodchar

Author
18 May 2006 9:44 PM
Mark Williams
SELECT Employee,
CASE
  WHEN Salary BETWEEN 35000 AND 40000 THEN 1
  WHEN Salary BETWEEN 40001 AND 45000 THEN 2
..
..
..
END AS "Category"
FROM [Your Table]

Or, you could have a table that stores that salary categories, and JOIN to it

SELECT e.Employee, c.Category
FROM [Your Table] e INNER JOIN [Salary Categories] c
ON e.Salary BETWEEN c.StartingSalary and c.EndingSalary

--

Show quote
"rodchar" wrote:

> hey all,
>
> what's the best way to express this in a query?
>
> for each employee
> take the salary and determine which range the particular salary falls in.
> for instance:
> 40k
> falls between 35-40k so the category is 1
>
> number of categories are 1-12
>
> thanks,
> rodchar
Author
18 May 2006 9:46 PM
BurgerKING
select case when salary < 40 and salary > 35 then 1
              when salary >= 40 and salary < x then 2
              when salary >= x and salary < y then 3
              ...
              when salary > z then 12
           end as 'category'
Author
18 May 2006 11:55 PM
Roy Harvey
Besides the CASE examples posted, consider a table of categories with
their ranges, and JOIN to it with the range test:

FROM Employees JOIN Ranges
ON Employees.salary >= Ranges.RangeMin
AND Employees.salary < Ranges.RangeMax

Roy


On Thu, 18 May 2006 14:35:01 -0700, rodchar
<rodc***@discussions.microsoft.com> wrote:

Show quote
>hey all,
>
>what's the best way to express this in a query?
>
>for each employee
>take the salary and determine which range the particular salary falls in.
>for instance:
>40k
>falls between 35-40k so the category is 1
>
>number of categories are 1-12
>
>thanks,
>rodchar
Author
22 May 2006 3:23 PM
BurgerKING
nice clean solution!! thanks.
Author
19 May 2006 1:36 AM
rodchar
thanks everyone for the help. i appreciate it a lot.

Show quote
"rodchar" wrote:

> hey all,
>
> what's the best way to express this in a query?
>
> for each employee
> take the salary and determine which range the particular salary falls in.
> for instance:
> 40k
> falls between 35-40k so the category is 1
>
> number of categories are 1-12
>
> thanks,
> rodchar

AddThis Social Bookmark Button