I'm not very familiar with SQL Server.
I have 3 tables, which relate to each other as one to many. So for one History, there are many Applications, and one Application can have many ApplicationAttributes.
I need to generate additional rows between dates range if for dates isn't exist any rows in database. If for given Imei already exist record, then need to take enrollTotalToday from previous row. For Id need to take last Id and just increment it. If for given dates ranges didn't exist previous rows, then just need to paste 0.
Please can anybody help me?
Histories table:
Id  Imei                  CreationDate   DeviceId
--------------------------------------------------
1   ProductionDevice299   2018-11-04     1
7   ProductionDevice299   2018-11-07     1
Applications table:
Id  Name              DeviceHistoryId
--------------------------------------
1   Enrollment.cone   1
2   DPC_OWNERS        1
3   OTHER_APPS        1
6   Enrollment.emp    7
7   DPC_OWNERS        7
ApplicationAttributes table:
Id  Key             Value   DeviceApplicationId
------------------------------------------------
1   EnrolledTotal   2       1
2   LoginsTotal     5       2
3   OtherAttribt1   8       3
4   OtherAttribt2   12      3
5   OtherAttribt3   17      3
6   EnrolledTotal   21      6
7   LoginsTotal     25      7
Expected result for range(2018-11-02, 2018-11-09):
Id  Imei                CreationDate DeviceId EnrollTotalToday EnrollTotalYesterday
8   ProductionDevice299 2018-11-02   1        0                0
9   ProductionDevice299 2018-11-03   1        0                0
1   ProductionDevice299 2018-11-04   1        2                0
10  ProductionDevice299 2018-11-05   1        2                0
11  ProductionDevice299 2018-11-06   1        2                0
7   ProductionDevice299 2018-11-07   1        21               2
12  ProductionDevice299 2018-11-08   1        21               0
13  ProductionDevice299 2018-11-09   1        21               0
My current SQL query:
WITH [CTE] AS 
(
    SELECT 
        [Extent1].[Id] AS [id], 
        [Extent1].[Imei] AS [imei], 
        CAST([Extent1].[CreationDate] AS DATE) AS [lastSeenOnline],  
        [Extent1].[DeviceId] AS [deviceId],
        SUM(CAST([Extent4].[Value] AS BIGINT)) as [enrollTotalToday],
        LAG(SUM(CAST([Extent4].[Value] AS BIGINT)), 1, 0) OVER (PARTITION BY [Extent1].[Imei] ORDER BY [Extent1].[CreationDate]) AS [enrollTotalYesterday]
    FROM 
        [DeviceManagement].[dbo].[DeviceHistory] AS [Extent1]
    INNER JOIN 
        (SELECT 
             [Imei], MAX([CreationDate]) AS MaxDate
         FROM 
             [dbo].[DeviceHistory]
         GROUP BY 
             [Imei], CAST([CreationDate] AS DATE)) [Extent2] ON [Extent1].[Imei] = [Extent2].[Imei] AND [Extent1].[CreationDate] = [Extent2].MaxDate
    INNER JOIN
        [DeviceManagement].[dbo].[DeviceApplication] AS [Extent3] ON [Extent3].DeviceHistoryId = [Extent1].id 
    INNER JOIN
        [DeviceManagement].[dbo].[DeviceApplicationAttribute] [Extent4] ON [Extent4].DeviceApplicationId = [Extent3].id AND [Extent4].[Key] = 'EnrolledTotal'
    GROUP BY 
        [Extent1].Id, [Extent1].Imei, [Extent1].CreationDate, [Extent1].DeviceId
) 
SELECT * 
FROM [CTE]
WHERE ([CTE].[lastSeenOnline] >= Convert(datetime, '2018-11-02' )) 
  AND ([CTE].[lastSeenOnline] <= Convert(datetime, '2018-11-09' ))
 
     
     
    
