与使用sql的今天int相比,如何获取昨天的int总和

时间:2018-11-10 15:20:30

标签: sql sql-server tsql

我对SQL不太熟悉。

我使用MS SQL。

我有3个表,每个表一对一地关联。对于一个历史记录,平均数具有许多应用程序,但是对于一个应用程序,均值具有许多ApplicationAttributes。

我需要在“历史记录”请求中为每个历史记录的应用程序显示EnrollTotalYesterday列,其中名称LIKE ='%Enrollment%'。

请有人可以帮助我吗?

历史记录表:

Id  Imei                  CreationDate   DeviceId
1   ProductionDevice299   2018-11-04     1
7   ProductionDevice299   2018-11-05     1

应用程序表:

Id  Name              DeviceHistoryId
1   Enrollment.cone   1
2   DPC_OWNERS        1
3   OTHER_APPS        1
6   Enrollment.emp    7
7   DPC_OWNERS        7

ApplicationAttributes表:

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

预期结果:

Id  Imei                CreationDate DeviceId EnrollTotalToday EnrollTotalYesterday
1   ProductionDevice299 2018-11-04   1        2                0
7   ProductionDevice299 2018-11-05   1        21               2

我当前的sql查询:

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 [enrolledTotalToday]

    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

    JOIN
     [DeviceManagement].[dbo].[DeviceApplication] AS [Extent3]
     ON [Extent3].DeviceHistoryId = [Extent1].id 
        JOIN
        [DeviceManagement].[dbo].[DeviceApplicationAttribute] [Extent4]
        on [Extent4].DeviceApplicationId = [Extent3].id and [Extent4].[Key] = 'EnrolledTotal'
    GROUP BY [Extent1].Id, [Extent1].Imei, [Extent1].CreationDate, [Extent1].DeviceId

1 个答案:

答案 0 :(得分:0)

如果您拥有SQL Server 2012+,这是一个简单的任务:

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 [enrolledTotalToday],

 -- simply add a LAG to get the previous row's value
    LAG(SUM(CAST(Extent4.Value AS BIGINT)), 1, 0)  -- previous row, default 0
    OVER (PARTITION BY Imei --???    
          ORDER BY 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

    JOIN
     [DeviceManagement].[dbo].[DeviceApplication] AS [Extent3]
     ON [Extent3].DeviceHistoryId = [Extent1].id 
        JOIN
        [DeviceManagement].[dbo].[DeviceApplicationAttribute] [Extent4]
        on [Extent4].DeviceApplicationId = [Extent3].id and [Extent4].[Key] = 'EnrolledTotal'
    GROUP BY [Extent1].Id, [Extent1].Imei, [Extent1].CreationDate, [Extent1].DeviceId

顺便说一句,您认为没有所有这些方括号[...]会更容易阅读吗?您仅应在实际需要时添加它们(然后可能会更好地使用Standard SQL双引号"...")。

每个表的别名为Extentn类似于tnabc,很难将其与表名相关联。而且,如果您删除表格,则在编号上会出现空格:-)您应该使用表格名称的缩写,例如DeviceHistory as HistDeviceApplication as Appl等。