| Author |
Topic |
|
pnasz
Posting Yak Master
101 Posts |
Posted - 2010-11-08 : 02:48:50
|
| you guys help me in solving this query but there is still one problem can u help me-- create sample datadeclare @sample table (FnType tinyint, EntryDate varchar(10), EntryTime varchar(8), EmpNo int)insert @sampleselect 1, '01/01/2010', '10:48:09', 13 union allselect 2, '01/01/2010', '13:48:09', 13 union allselect 1, '02/01/2010', '10:10:09', 13 union allselect 2, '02/01/2010', '12:48:09', 13 union allselect 1, '02/01/2010', '09:45:00', 22select * from @sampleselectt1.EmpNo, t1.EntryDate,t1.EntryTime as Timein,isnull(t2.EntryTime,'Absent') as timeoutfrom @sample as t1left join @sample as t2on t1.EmpNo=t2.EmpNoand t1.EntryDate=t2.EntryDateand t1.FnType=1and t2.FnType=2Where t1.Fntype=1this is working and showing absent for emp 22 in timeoutbut what if there is timeout for emp 23 and i want to show null for time indeclare @sample table (FnType tinyint, EntryDate varchar(10), EntryTime varchar(8), EmpNo int)insert @sampleselect 1, '01/01/2010', '10:48:09', 13 union allselect 2, '01/01/2010', '13:48:09', 13 union allselect 1, '02/01/2010', '10:10:09', 13 union allselect 2, '02/01/2010', '12:48:09', 13 union allselect 2, '02/01/2010', '13:45:00', 23 union allselect 1, '02/01/2010', '09:45:00', 22 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-08 : 03:53:52
|
| [code]declare @sample table (FnType tinyint, EntryDate varchar(10), EntryTime varchar(8), EmpNo int)insert @sampleselect 1, '01/01/2010', '10:48:09', 13 union allselect 2, '01/01/2010', '13:48:09', 13 union allselect 1, '02/01/2010', '10:10:09', 13 union allselect 2, '02/01/2010', '12:48:09', 13 union allselect 2, '02/01/2010', '13:45:00', 23 union allselect 1, '02/01/2010', '09:45:00', 22;with cteas(selectt1.EmpNo, t1.EntryDate,t1.EntryTime as Timein,isnull(t2.EntryTime,'Absent') as Timeoutfrom @sample as t1left join @sample as t2on t1.EmpNo=t2.EmpNoand t1.EntryDate=t2.EntryDateand t1.FnType=1and t2.FnType=2Where t1.Fntype=1)select * from cteunion allselect EmpNo,entrydate,null,EntryTime from @sample s where not exists(select * from cte where s.EmpNo=cte.EmpNo)[/code]PBUH |
 |
|
|
pnasz
Posting Yak Master
101 Posts |
Posted - 2010-11-08 : 05:34:46
|
| Thanx a lot but how do i insert this select into another table |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-08 : 05:41:53
|
| [code]declare @sample table (FnType tinyint, EntryDate varchar(10), EntryTime varchar(8), EmpNo int)insert @sampleselect 1, '01/01/2010', '10:48:09', 13 union allselect 2, '01/01/2010', '13:48:09', 13 union allselect 1, '02/01/2010', '10:10:09', 13 union allselect 2, '02/01/2010', '12:48:09', 13 union allselect 2, '02/01/2010', '13:45:00', 23 union allselect 1, '02/01/2010', '09:45:00', 22;with cteas(selectt1.EmpNo, t1.EntryDate,t1.EntryTime as Timein,isnull(t2.EntryTime,'Absent') as Timeoutfrom @sample as t1left join @sample as t2on t1.EmpNo=t2.EmpNoand t1.EntryDate=t2.EntryDateand t1.FnType=1and t2.FnType=2Where t1.Fntype=1), cte1as(select * from cteunion allselect EmpNo,entrydate,null,EntryTime from @sample s where not exists(select * from cte where s.EmpNo=cte.EmpNo))insert into @yourtableselect * from cte1[/code]PBUH |
 |
|
|
pnasz
Posting Yak Master
101 Posts |
Posted - 2010-11-08 : 06:55:49
|
| I tried this with real data but for date 2010-09-01 and date 2010-09-09 its not showing null for timeinplease helpdeclare @sample table (FnType tinyint, EntryDate varchar(10), EntryTime varchar(8), EmpNo int)insert @sampleSELECT 02,'2010-09-01','16:33:40',7930 union all SELECT 01,'2010-09-04','09:12:17',7930 union all SELECT 02,'2010-09-04','17:09:34',7930 union all SELECT 01,'2010-09-05','09:33:13',7930 union all SELECT 02,'2010-09-05','17:26:16',7930 union all SELECT 01,'2010-09-06','09:08:03',7930 union all SELECT 02,'2010-09-06','16:37:20',7930 union all SELECT 02,'2010-09-09','16:37:20',7930 union all SELECT 01,'2010-09-07','09:12:16',7930 --select * from @sample;with cteas(selectt1.EmpNo, t1.EntryDate,t1.EntryTime as Timein,isnull(t2.EntryTime,'Absent') as Timeoutfrom @sample as t1left join @sample as t2on t1.EmpNo=t2.EmpNoand t1.EntryDate=t2.EntryDateand t1.FnType=1and t2.FnType=2Where t1.Fntype=1)select * from cteunion allselect EmpNo,entrydate,null,EntryTime from @sample s where not exists(select * from cte where s.EmpNo=cte.EmpNo) |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-08 : 07:24:41
|
Because in your initial requirement you had different empno and different dates but in your current sample data you have same empnobut different datesdeclare @sample table (FnType tinyint, EntryDate varchar(10), EntryTime varchar(8), EmpNo int)insert @sampleSELECT 02,'2010-09-01','16:33:40',7930 union all SELECT 01,'2010-09-04','09:12:17',7930 union all SELECT 02,'2010-09-04','17:09:34',7930 union all SELECT 01,'2010-09-05','09:33:13',7930 union all SELECT 02,'2010-09-05','17:26:16',7930 union all SELECT 01,'2010-09-06','09:08:03',7930 union all SELECT 02,'2010-09-06','16:37:20',7930 union all SELECT 02,'2010-09-09','16:37:20',7930 union all SELECT 01,'2010-09-07','09:12:16',7930 select * from @sample order by EntryDate,FnType;with cteas(selectt1.EmpNo, t1.EntryDate,t1.EntryTime as Timein,isnull(t2.EntryTime,'Absent') as Timeoutfrom @sample as t1left join @sample as t2on t1.EmpNo=t2.EmpNoand t1.EntryDate=t2.EntryDateand t1.FnType=1and t2.FnType=2Where t1.Fntype=1)select * from cteunion allselect EmpNo,entrydate,null,EntryTime from @sample s where not exists(select * from cte where s.EmpNo=cte.EmpNo and s.EntryDate=cte.EntryDate) PBUH |
 |
|
|
|
|
|