Wednesday, March 7, 2012

complex SELECT Statement

TABLE : USER
USERID
1
2

TABLE : TIME
TIMEID|USERID|RT|OT|DOT|DATE
1|1|8|2|1|2004-02-01
2|1|8|2|0|2004-02-02

3|2|8|0|0|2004-02-01
4|2|8|2|2|2004-02-02

RT : Regular Time
OT : Over-Time
DOT : Double Over-Time

I need to write a query to display the results in this way

USERID|DATE|TIME
1|2004-02-01|8
1|2004-02-01|2
1|2004-02-01|1

1|2004-02-02|8
1|2004-02-02|2

2|2004-02-01|8
2|2004-02-02|8
2|2004-02-02|2
2|2004-02-02|2

basically, the time entries for each user each day , seperate rows for RT, OT, DOT if they are not equal to 0

ive been breaking my head on this for quite a while. any help is appreciated.

thankscan you detail the TIME column in your result

what do you want in this column ?|||never mind my last post

just understood...

I'm checking for the query now|||this should do

sort the query in the wanted order

select date, users.userid, time from users
inner join (
select date, userid, rt as time from time where rt <>'0'
union
select date, userid, ot from time where ot <>'0'
union
select date, userid, dot from time where dot <>'0') detail_time
on detail_time.userid=users.userid
order by date, users.userid asc|||thanks karolyn,
u saved my day !!

reg
db_montreal|||and it's only starting in montreal

No comments:

Post a Comment