Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have two tables,
first one is "tbl_login" which contains two columns "userid' and "name" (both are varchar fields)
second one is "tbl_leave" which conatins three columns "userid","fromdate","duration" (all are varchar fields)
tbl_login.userid=tbl_leave.userid

for example
tbl_login :

userid name
001 abcd
002 defc

tbl_leave

userid fromdate duration
001 2011/03/08 2 days
002 2011/03/28 5 days

i want a query to display like this

userid name status
001 abcd absent
002 defc present

display all names in tbl_login first,then check any userid having today date in fromdate column of tbl_leave
if existing then Absent,else Present to be displayed


Please help
Posted
Updated 7-Mar-11 18:40pm
v3

1 solution

Try this--
SQL
select userid, name, (Case when Exist(Select 1 from tbl_leave where tbl_leave.UserID=tbl_login.userid and Convert(varchar(11),tbl_leave.fromdate,106)=convert(varchar(11),getdate(),106)) then 'absent' else 'present' end) as status from tbl_login


--Pankaj
 
Share this answer
 
Comments
kishore Rajendran 10-Mar-11 1:13am    
i want to check that tbl.leave.fromdate in a date range...i tried but getting this error
"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

Please help
pankajupadhyay29 10-Mar-11 1:19am    
what query your writing??
kishore Rajendran 10-Mar-11 1:22am    
SELECT tbl_login.userid,name,ISNULL ((SELECT 'Absent ' AS Expr1 FROM tbl_leave WHERE (userid = tbl_login.userid) AND fromdate = '2011/03/10'), 'Present'),ISNULL ((SELECT type AS Expr1 FROM tbl_leave WHERE (userid = tbl_login.userid) AND fromdate = '2011/03/10'), null) as status from tbl_login
kishore Rajendran 10-Mar-11 1:30am    
SELECT tbl_login.userid,name,ISNULL ((SELECT 'Absent ' AS Expr1 FROM tbl_leave WHERE (userid = tbl_login.userid) AND '2011/03/10' between '2011/03/10' and '2011/07/03'), 'Present'),ISNULL ((SELECT type AS Expr1 FROM tbl_leave WHERE (userid = tbl_login.userid) AND fromdate = '2011/03/10'), null) as status from tbl_login
pankajupadhyay29 10-Mar-11 1:43am    
use case when with exist to achieve this as i used in the solution for otherwise whenever it is returning multiple values it will give error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900