| Author |
Topic |
|
Osangar
Starting Member
7 Posts |
Posted - 2005-04-06 : 09:00:51
|
| Hi chaps,I was hoping someone could help me with this query pleaseI need to find the maximum salaries of male and female staff. I have a staff table with staff salaries in one column and another column with staff sex.I have tried the following, but it does not work and I cant seem to get my brain around what seems like a simple query.Select distinctmale Staff Salary = (select MAX(salary)where staff.sex = 'M')female Staff Salary = (select MAX(salary)where staff.sex = 'F')from staffany assistance would be greatly apreciated |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-04-06 : 09:05:33
|
try something like this:select MAX(Case when sex = 'M' then Salary else 0 End) as MaxMaleSalary, MAX(Case when sex = 'F' then Salary else 0 End) as MaxFemaleSalary, MAX(Case when sex = 'Not Sure' then Salary else 0 End) as MaxNotSureSalary from YourTable by the way, when you ask a question and say "something doesn't work" it is important to say what you mean by that .. does it return errors? does it return wrong data? does it even compile? etc... It's like going to a mechanic and saying "my car doesn't work .. how much will it take to fix it?"- Jeff |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-04-06 : 09:08:02
|
[code]select m_salary = (select max(salary) from staff where sex = 'm'), f_salary = (select max(salary) from staff where sex = 'f')[/code]EDIT: yeah! what Dr. said!!Go with the flow & have fun! Else fight the flow |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-04-06 : 09:13:48
|
spirit, you forgot the 3rd possibility ! - Jeff |
 |
|
|
Osangar
Starting Member
7 Posts |
Posted - 2005-04-06 : 09:15:18
|
| That has worked a treat; I really appreciate your assistance spirit1. Ahh yes sorry jsmith8858 that is a good point.Cheers, for both the code and guidance. Have a great day |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-04-06 : 15:07:43
|
| This will take care of all the possibilities, wouldn't it? :)Select sex, max(salary) as maxsalary from YourTable group by sexBe One with the OptimizerTG |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-04-06 : 15:08:58
|
| depends how you want the results to be returned, in multiple rows or multiple columns.- Jeff |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-04-06 : 15:14:00
|
| Yeah, I just wanted to make sure Osanger realized it was a possiblility. (plus anytime I can type "group sex" is just a bonus)Be One with the OptimizerTG |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-04-06 : 15:15:36
|
ROTFL!!!!!!      Go with the flow & have fun! Else fight the flow |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-04-06 : 15:31:29
|
Yes the results are in we all need toGet a real life!Oh yea and Group sex is overrated! JimUsers <> Logic |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-04-06 : 15:42:32
|
SEX & PROFANITY ! WHERE WILL IT ALL END??!   Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
|