Thank you Luis and Sean.
Here is my try
use tempdb;
set nocount on;
if OBJECT_ID('dbo.TallyTable') is not null drop table dbo.TallyTable
go
select top 100 IDENTITY(int,1,1) as ID
into dbo.TallyTable from master.dbo.syscolumns
alter table dbo.TallyTable add constraint pk_tallytableId primary key clustered(ID)
GO
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 ID, DATEADD(DAY, ID - 1, '2013-08-01') as MyDate
from dbo.TallyTable t
where t.ID <= 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
the "MyNewValue" data has to come in the place of null on recordscount column
also i need the output as pivotized. any help please
also am confused about the result as my expected results was column should be as dates and row as recordscount and if recordcount is null then whatever we discussed about applying the formula to bring the dates