Using SQL Server 2000
dates 2012-08-02 2012-08-19 2012-08-20
id dates time 001 01/08/2012 00:00 001 02/08/2012 00:00 001 03/08/2012 00:00 001 04/08/2012 12:00 ... ... 001 17/04/2012 11:00 001 18/08/2012 00:00 001 19/08/2012 00:00 001 20/08/2012 00:00 001 21/08/2012 12:00 ...
I want to check previous date and next date of each column, then I want to update present or absent or holiday.
Note: this query is running witout any fail, only concern is time consuming...
Select t2.id, t2.dates, case when t2.time <> '00:00' then 'P' when t4.dates = t2.dates and t1.intime = '00:00' and t3.intime = '00:00' then 'AB' else 'H' end as attn from ( Select id, dates, time from table1 where t1.dates = Cast('18/08/2012' as datetime) ) t1 left outer join ( Select id, dates, time from table1 where t2.dates = Cast('19/08/2012' as datetime) ) t2 on t1.id = t2.id left outer join ( Select id, dates, time from table1 where t2.dates = Cast('20/08/2012' as datetime) ) t3 on t2.id = t3.id left outer join ( select dates from holiday ) t4 on t4.dates = t2.dates
The above query is working fine, but it was taking more time to display because I want to view the data from
30/09/2012 for each
id, i have n number of id,
system is checking previous date and next date for each id and showing the result.
Any other alternative query or solution is there for displaying the data
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
CREATE INDEX holiday_idx_dates ON holiday (dates); CREATE INDEX table1_idx_dates_id ON table1 (dates,id);
SELECT t2.id, t2.dates, CASE WHEN t2.time <> '00:00' THEN 'P' WHEN t4.dates = t2.dates AND t1.intime = '00:00' AND t3.intime = '00:00' THEN 'AB' ELSE 'H' END AS attn FROM table1 t1 LEFT OUTER JOIN table1 t2 ON t1.id = t2.id AND t2.dates = CAST('19/08/2012' AS datetime) LEFT OUTER JOIN table1 t3 ON t2.id = t3.id AND t2.dates = CAST('20/08/2012' AS datetime) LEFT OUTER JOIN holiday t4 ON t4.dates = t2.dates WHERE ( t1.dates = CAST('18/08/2012' AS datetime) )