0

I have a query like below:

Select Lname, MAX(HOURS)    
From (Select E.LName, SUM(Hours) as HOURS    
From WORKS_ON W    
     JOin EMPLOYEE E on E.SSN=W.ESSN    
Group by E.SSN) as hours

The answer is like:

Lname MAX(HOURS)

Black 55

From DB 55 is right answer for the max hour. But Black is not working 55 hours, it is another man called Grant. It seems like I have an answer that do not relate to each other. What should I do in order to get like:

"Grant 55" as an answer.

3
  • maybe, u can explain what I'm doing wrong??? Commented Nov 2, 2015 at 11:10
  • please add info about your tables ... Commented Nov 2, 2015 at 11:24
  • Please provide some sample data or create a fiddle Commented Nov 2, 2015 at 11:33

2 Answers 2

1
Select E.LName, SUM(Hours) as HOURS    
From WORKS_ON W    
   JOin EMPLOYEE E on E.SSN=W.ESSN    
group by E.LName
ORDER BY HOURS desc
limit 1
Sign up to request clarification or add additional context in comments.

2 Comments

I've got a result: Black 604, I think I need to add group by to your code
@NurdauletKenges, yes, of course a GROUP BY is required... I've edited.
0

I am assuming distinct LName,SSN in the E table

Select 
    E.LName
    ,SUM(Hours) as HOURS    
From 
    WORKS_ON W    
    JOin EMPLOYEE E on E.SSN=W.ESSN 
GROUP BY
    E.SSN
    ,E.LName   
ORDER BY 
    E.LName

You may want to change the order by clause according to your requirement.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.