| Author |
Topic |
|
sindhu sarah
Starting Member
34 Posts |
Posted - 2011-10-30 : 04:34:23
|
| Hi alli need a solution for updating the table.There are 2 tables.one tables has date column with id's.The other table has 2 date column.one is start date and other is end date.The first table date column needs to be saved with2 date column.For exampl Table A has a date column with data's as follows 2011-08-07 00:00:00 2011-08-08 00:00:00 2011-08-09 00:00:00 2011-08-10 00:00:00 2011-08-11 00:00:00 2011-08-15 00:00:00The above column need to be saved in table B as start date End date 2011-08-07 00:00:00 2011-08-11 00:00:00 2011-08-15 00:00:00 2011-08-15 00:00:00It's like column needs to be saved lik a consecutive date's together.This is for Leave application i.e the employee is leave from7 th to 11 th of august and again he is on leave on 15 th of august.Hope it is clear.Kindly let me know a good solution and thanks in advance for the help. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-30 : 05:52:25
|
| [code]declare @tbl table(dateval datetime)insert @tblselect '07/08/2011 00:00:00' union allselect '08/08/2011 00:00:00' union allselect '09/08/2011 00:00:00' union allselect '10/08/2011 00:00:00' union allselect '11/08/2011 00:00:00' union allselect '15/08/2011 00:00:00' union allselect '18/08/2011 00:00:00' union allselect '19/08/2011 00:00:00' union allselect '23/08/2011 00:00:00' union allselect '25/08/2011 00:00:00' union allselect '26/08/2011 00:00:00' union allselect '27/08/2011 00:00:00' union allselect '30/08/2011 00:00:00' ;With CTEAS(SELECT ROW_NUMBER() OVER (ORDER BY dateval ASC) AS Rn,*FROM @tbl),Dates_CTEAS(SELECT TOP 1 Rn,dateval,1 AS GrpFROM CTEUNION ALLSELECT t.Rn,t.dateval,CASE WHEN DATEDIFF(dd,c.dateval,t.dateval)=1 THEN c.Grp ELSE c.Grp + 1 ENDFROM Dates_CTE cINNER JOIN CTE tON t.Rn= c.Rn+1 ) SELECT MIN(dateval) AS Start,MAX(dateval) AS [End] FROM Dates_CTE GROUP BY Grpoutput-------------------------------------------------------Start End2011-08-07 00:00:00.000 2011-08-11 00:00:00.0002011-08-15 00:00:00.000 2011-08-15 00:00:00.0002011-08-18 00:00:00.000 2011-08-19 00:00:00.0002011-08-23 00:00:00.000 2011-08-23 00:00:00.0002011-08-25 00:00:00.000 2011-08-27 00:00:00.0002011-08-30 00:00:00.000 2011-08-30 00:00:00.000[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sindhu sarah
Starting Member
34 Posts |
Posted - 2011-10-30 : 07:51:24
|
| Thanks a lot for the reply.The O/P seems to look the same what i was looking for.But when am excuting in SQL 2008,it throws an error.Msg 242, Level 16, State 3, Line 5The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.The statement has been terminated.(0 row(s) affected) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-30 : 10:04:56
|
quote: Originally posted by sindhu sarah Thanks a lot for the reply.The O/P seems to look the same what i was looking for.But when am excuting in SQL 2008,it throws an error.Msg 242, Level 16, State 3, Line 5The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.The statement has been terminated.(0 row(s) affected)
pass date values in yyyy-mm-dd format and you wont have this issue------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sindhu sarah
Starting Member
34 Posts |
Posted - 2011-10-31 : 01:53:00
|
| Thanks for the solution.But still i am facing problem.Because this is for attendance type.For exampl an employee is on leave from 1st to 31st of august.so when i execute the above query it shows Start End2011-08-01 00:00:00.000 2011-08-31 00:00:00.000But the problem is the employees is on annual vacation from 2011-08-01 00:00:00.000 to 2011-08-15 00:00:00.000 and remaining days he is leave without pay.For ex:Dates attendance2011-08-01 00:00:00.000 A2011-08-02 00:00:00.000 Aetc... A2011-08-15 00:00:00.000 A2011-08-16 00:00:00.000 LWP2011-08-17 00:00:00.000 LWPetc.. LWP2011-08-31 00:00:00.000 LWPNow the result should be showm asGroup by dates and attendance as well-------------------------------------------------------Start End2011-08-01 00:00:00.000 2011-08-15 00:00:00.0002011-08-16 00:00:00.000 2011-08-31 00:00:00.000Thanks in advance for your kind reply |
 |
|
|
sindhu sarah
Starting Member
34 Posts |
Posted - 2011-10-31 : 03:10:01
|
| Ya i grouped it with attendance as well..now it work's.I will check it with all other employee's and check for the issue.Thanks a lot for the solution visakh. |
 |
|
|
sindhu sarah
Starting Member
34 Posts |
Posted - 2011-10-31 : 03:10:09
|
| Ya i grouped it with attendance as well..now it work's.I will check it with all other employee's and check for the issue.Thanks a lot for the solution visakh. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-31 : 04:15:24
|
quote: Originally posted by sindhu sarah Ya i grouped it with attendance as well..now it work's.I will check it with all other employee's and check for the issue.Thanks a lot for the solution visakh.
No problem you're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|