• born2achieve (2/14/2014)


    yes, all my output data can be text. any help please

    I was hoping you would say no. It is much easier to do date math with actual dates.

    I know this isn't a complete solution but here is a way to get most of it by using a tally table. You can read about tally tables here. http://www.sqlservercentral.com/articles/62867/[/url]

    if OBJECT_ID('tempdb..#company') is not null

    drop table #company

    create table #company

    (

    Name varchar(20),

    DaysInLate int

    )

    insert #company

    select 'microsoft' as Name, 15 as daysinlate union all

    select 'nokia' as name, 10 as daysinlate union all

    select 'Google' as name, 13 as daysinlate

    if OBJECT_ID('tempdb..#data') is not null

    drop table #data

    create table #data

    (

    Name varchar(20),

    DataReceived datetime,

    RecordsCount int

    )

    insert #data

    select 'microsoft' as Name, '2013-08-01' as datareceived, 1000 as recordscount union all

    select 'microsoft' as Name, '2013-08-02' as datareceived, 1001 as recordscount union all

    select 'microsoft' as Name, '2013-08-03' as datareceived, 1002 as recordscount union all

    select 'microsoft' as Name, '2013-08-04' as datareceived, 1003 as recordscount union all

    select 'microsoft' as Name, '2013-08-05' as datareceived, 1005 as recordscount union all

    select 'microsoft' as Name, '2013-08-06' as datareceived, 1005 as recordscount union all

    select 'microsoft' as Name, '2013-08-07' as datareceived, 1006 as recordscount union all

    select 'microsoft' as Name, '2013-08-08' as datareceived, 1007 as recordscount union all

    select 'microsoft' as Name, '2013-08-09' as datareceived, 1004 as recordscount union all

    select 'microsoft' as Name, '2013-08-10' as datareceived, 1033 as recordscount union all

    select 'microsoft' as Name, '2013-08-11' as datareceived, 1020 as recordscount;

    ;

    with MyDates as

    (

    select N, DATEADD(DAY, N - 1, '2013-08-01') as MyDate

    from tally t

    where t.N <= 31

    )

    select *, case when DataReceived IS not null then DataReceived else (select top 1 DATEADD(DAY, c.DaysInLate, md.MyDate) from #data d2 join #company c on d2.Name = c.Name where c.Name = 'microsoft') end as MyNewValue

    from MyDates md

    left join #data d on d.DataReceived = md.MyDate

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/