views:

60

answers:

1

We are capturing attendance data in a certain table primarily capturing the values (userId, startDate, endDate). If a person applies leave on a certain date then only startDate is filled and if he applies for a set of dates then the start/end dates are captured. What query will help me find if a person is taking leave on a certain date (say on the 9th, 12th, 15th of this month)

user1, 9/8/2010
user2, 9/9/2010, 9/10/2010
user3, 9/14/2010
user4, 9/15/2010
user5, 9/9/2010, 9/20/2010

users taking leaving on 9th would be user2, user5
users taking leaving on 12th would be user5
users taking leaving on 15th would be user4, user5

The query should not contain database specific constructs, it needs to run on h2 / mysql / postgres using hibernate as the ORM layer

+6  A: 
SELECT  userId
FROM    mytable
WHERE   '2010-09-09' BETWEEN startDate AND COALESCE(endDate, startDate)
Quassnoi
we use hibernate as the ORM layer, is the coalesce command portable across databases
Samuel
Its a standard and works in `MySQL`, `PostgreSQL`, `Oracle` and `SQL Server`. Don't know about `H2`, though.
Quassnoi
it works on h2 also, thanks
Samuel
What does `coalesce` do, exactly?
Loadmaster
@Loadmaster: returns first non-`NULL` argument
Quassnoi