PDA

View Full Version : Need help with query


shrinath
04-29-2008, 11:22 AM
I need help on query. I hope somebody can help me.

Table1 : employee
empid | name
-----------------------
1 | David
2 | Robert
3 | Anton
4 | Stewart
5 | John

Table2 : employee_activity
empid | time | activity
----------------------------------------------------------
1 | 2008-04-22 09:00:00 | Tea
2 | 2008-04-22 09:00:00 | Tea
3 | 2008-04-22 09:00:00 | In seat
4 | 2008-04-22 09:00:00 | Tea
5 | 2008-04-22 09:00:00 | Meeting
1 | 2008-04-22 10:00:00 | Taking Interview
2 | 2008-04-22 10:00:00 | Meeting
3 | 2008-04-22 10:00:00 | In seat
4 | 2008-04-22 10:00:00 | In seat
5 | 2008-04-22 10:00:00 | Meeting
1 | 2008-04-22 11:00:00 | Taking Interview
2 | 2008-04-22 11:00:00 | In seat
3 | 2008-04-22 11:00:00 | In seat
4 | 2008-04-22 11:00:00 | Meeting
5 | 2008-04-22 11:00:00 | Meeting


I want result something like this :

name | activityat9 | activityat10 | activityat11
-----------------------------------------------------------------------------------------
David | Tea | Taking Interview | Taking Interview
Robert | Tea | Meeting | In seat
Anton | In seat | In seat | In seat
Stewart | Tea | In seat | Meeting
John | Meeting | Meeting | Meeting

I hope the requirement is clear. Thx in advance.

Stooshie
04-29-2008, 04:36 PM
SELECT e.name, ea.activity
FROM employee e
LEFT OUTER JOIN e.name
ON ea.empid = e.empid
GROUP BY e.name, ea.time

This will return

David|Tea
David|Taking Interview
David|Taking Interview
Robert|Tea
Robert|Meeting
.
.

You then use the PHP, C++, VB whatever, to loop round the rows and display them the way you want.