| Author |
Topic |
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2005-02-08 : 19:40:05
|
| Here iam stuck to get a combination of query for reportings.....Create table tbl_emp_status(emp_id varchar(20),std_id int,status char(1))insert into tbl_emp_status(emp_id,std_id,status)values(1005370,2323,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1005370,2325,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1005370,2326,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054170,3339,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1054170,3337,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1054170,3446,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1054179,2339,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054179,2337,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054179,2446,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054199,337,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054199,446,'N')Here I need 3 types of query :-1.I need the emp_id and std_id with the combinations having just status as only 'Y'2.I need the emp_id and std_id with the combinations having just status as only 'N'3.I need the emp_id and std_id with the combinations having just status as 'N' and only one 'Y'case 1 should give 1054179,2339,'Y'1054179,2337,'Y'1054179,2446,'Y'case 2 should give 1054170,3339,'N'1054170,3337,'N'1054170,3446,'N'case 3 should give1005370,2323,'N'1005370,2325,'N'1005370,2326,'Y'1054199,337,'Y'1054199,446,'N' |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-02-08 : 19:54:11
|
Here ya go:set nocount onCreate table tbl_emp_status(emp_id varchar(20),std_id int,status char(1))insert into tbl_emp_status(emp_id,std_id,status)values(1005370,2323,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1005370,2325,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1005370,2326,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054170,3339,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1054170,3337,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1054170,3446,'N')insert into tbl_emp_status(emp_id,std_id,status)values(1054179,2339,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054179,2337,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054179,2446,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054199,337,'Y')insert into tbl_emp_status(emp_id,std_id,status)values(1054199,446,'N')select emp_id, std_id, statusfrom tbl_emp_statuswhere status = 'Y' and emp_id not in (select emp_id from tbl_emp_status where status = 'N')select emp_id, std_id, statusfrom tbl_emp_statuswhere status = 'N' and emp_id not in (select emp_id from tbl_emp_status where status = 'Y')select t1.emp_id, t1.std_id, t1.statusfrom tbl_emp_status t1inner join ( select emp_id from tbl_emp_status where status = 'y' group by emp_id having count(*) = 1) t2 ON t1.emp_id = t2.emp_iddrop table tbl_emp_status Tara |
 |
|
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2005-02-08 : 22:38:17
|
| It worked tara...Thanks a lot........... |
 |
|
|
|
|
|