Hi there, I have the following table struction, lets call it table A.bookid startdate endate 2001 2000-01-01 2000-01-053001 2001-01-01 2001-01-024001 2002-01-01 2002-01-04and i want the end result to be look like this in table B.bookid startdate endate bookidrowdate bookidlogseq rowsequence 2001 2000-01-01 2000-01-05 2000_01_01 2001_0 02001 2000-01-01 2000-01-05 2000_02_01 2001_1 12001 2000-01-01 2000-01-05 2000_03_01 2001_2 22001 2000-01-01 2000-01-05 2000_04_01 2001_3 43001 2002-02-01 2003-02-02 2000_01_01 3001_0 03001 2002-02-01 2003-02-02 2000_02_01 3001_1 14001 2002-01-01 2002-01-04 2002-01-01 4001_0 04001 2002-01-01 2002-01-04 2002-02-01 4001_1 14001 2002-01-01 2002-01-04 2002-02-01 4001_2 2The script below works but i have a break when datediff (days,startdate, endate) reaches 0. For every bookidm i want to iterate till the datediff is zero then move on to next bookid and do the sam thing.declare @orders table( bookid int, startdate date, endate date, rowsequence int)insert @orders (bookid, startdate, endate) values(2001,'2000-01-01','2000-01-05'),(3001,'2001-01-01','2001-01-02'),(4001,'2002-01-01','2002-01-04')select *, CAST(bookid as CHAR(4)) + '_' + CAST(rowsequence as char(2)) bookidlogseq ,CASE WHEN rowsequence<= DATEDIFF(DAY, startdate, endate) THEN ---THIS IS NOT WORKING! DATEADD(mm, rowsequence, CONVERT(VARCHAR(10),startdate,112))--have this avoid arithmetic overflow ELSE '1900-01-01' END AS bookidrowdate from ( select bookid, startdate, endate, ROW_NUMBER() over(Partition by bookid order by startdate)-1 rowsequence from @orders ) ordany suggestions?
↧