If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with Group by clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-05-05, 02:33
Sudar Sudar is offline
Registered User
 
Join Date: Jul 2004
Location: Mars
Posts: 137
Help with Group by clause

I have two tables

First table

mstr_dept

Code:
  
  `deptid` int(11) NOT NULL primary key,
  `deptname` varchar(10) NOT NULL,
Second table


emp
Code:
  
  `empid` int(11) NOT NULL primary key,
  `deptid` int(11) refrences mstr_dept(deptid)
  `name` varchar(30) NOT NULL,
  `salary` int(11) NOT NULL default '0',

Now i have to select the maximum salary for each department

for which i can use the query

Code:
  
SELECT deptid, MAX(salary) from emp group by deptid
but now i want to know who is getting the maximum salary in each department. If i give the query

Code:
  
SELECT deptid, empid, MAX(salary) from emp group by deptid, empid
i am not getting the correct answer. So any one can help me..
__________________
Sudar

--
My Blog
Reply With Quote
  #2 (permalink)  
Old 11-14-05, 12:39
sassermann sassermann is offline
Registered User
 
Join Date: Aug 2002
Location: Germany
Posts: 17
Try something like this:

select e.deptid, e.empid, e.name, e.salary from emp e, (SELECT deptid, MAX(salary) sal from emp group by deptid ) max_sal where e.deptid=max_sal.deptid and e.salary=max_sal.sal
Reply With Quote
  #3 (permalink)  
Old 11-15-05, 05:32
Sudar Sudar is offline
Registered User
 
Join Date: Jul 2004
Location: Mars
Posts: 137
Thanks sassermann for the reply.....
__________________
Sudar

--
My Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On