选择在第10天记录的每个设备的最大记录

时间:2018-08-24 14:10:45

标签: sql sql-server sql-server-2017

我有一个表tblActiveList,其中包含“ DeviceID”和“ TimeAdded”列,另一个表是tblActiveBladeLive,其中包含DeviceID,RecordedDate和Angle列 如何为每个设备选择在第10天记录的最大角度(每个设备在任何一天都可以有多个角度)?

这是我的查询,选择添加设备的第10天

Select DeviceID, TimeAdded, TimeAdded+10 as '10TH_Day' from tblActiveList

DeviceID    TimeAdded                 10TH_Day
BL000343    2018-08-08 15:09:54.483   2018-08-18 15:09:54.483
BL000247    2018-08-08 17:03:57.593   2018-08-18 17:03:57.593

这是第二张桌子,其中添加了Day设备几个月以来的所有角度。

Select DeviceID, RecordedDate, Angle from tblActiveBladeLive

DeviceID    RecordedDate            Angle
BL000343    2018-08-15 23:55:13.000 11.50
BL000343    2018-08-16 22:54:58.000 12.55
BL000343    2018-08-16 21:54:58.000 12.55
BL000343    2018-08-17 23:54:59.000 13.15
BL000343    2018-08-18 05:54:59.000 15.15
BL000343    2018-08-18 01:54:59.000 13.15
BL000247    2018-08-17 03:44:57.000 15.78
BL000247    2018-08-18 06:46:41.000 15.05
BL000247    2018-08-17 11:46:56.000 15.05
BL000247    2018-08-18 05:46:41.000 14.05

这是需要选择的记录

DeviceID    RecordedDate            Angle
BL000343    2018-08-18 05:54:59.000 15.15
BL000247    2018-08-18 06:46:41.000 15.05

任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:1)

这就是我要做的:

SELECT A.DeviceID, MAX(RecordedDate) AS RecordedDate, A.Angle
FROM (SELECT tblActiveList.DeviceID, CONVERT(date,DATEADD(d,10,TimeAdded)) AS Day10, MAX(Angle) AS Angle
        FROM tblActiveList
            LEFT JOIN tblActiveBladeLive ON tblActiveList.DeviceID=tblActiveBladeLive.DeviceID AND CONVERT(date,DATEADD(d,10,TimeAdded))=CONVERT(date,RecordedDate)
        GROUP BY tblActiveList.DeviceID, TimeAdded) A
    LEFT JOIN tblActiveBladeLive B ON A.DeviceID=B.DeviceID AND A.Angle=B.Angle AND Day10=CONVERT(date,RecordedDate)
GROUP BY A.DeviceID, A.Angle

本文是在没有测试环境的情况下编写的,因此可能存在错别字和错误。在第10天很容易获得最大角度。然后,最困难的部分是获取与其对应的时间戳。如果您不在乎时间部分,而只关心最终结果中的日期部分,那么它会变得更加简单:

SELECT tblActiveList.DeviceID, CONVERT(date,DATEADD(d,10,TimeAdded)) AS Day10, MAX(Angle) AS Angle
FROM tblActiveList
    LEFT JOIN tblActiveBladeLive ON tblActiveList.DeviceID=tblActiveBladeLive.DeviceID AND CONVERT(date,DATEADD(d,10,TimeAdded))=CONVERT(date,RecordedDate)
GROUP BY tblActiveList.DeviceID, TimeAdded

答案 1 :(得分:0)

我对您的数据和所需内容感到困惑。您需要做的是至少使用一次JOIN,CTE,可能是GROUP BY。最有可能是相关子查询。如果您可以将您的问题改写得更好,那将会有所帮助。如果知道,还可以添加使用的SQL引擎。

答案 2 :(得分:0)

编辑1.有一个问题。

尝试一下。看起来上面提到了相同的方法,但是也许有用:

WITH device_10thDay as
(
Select 
    DeviceID, CONVERT(VARCHAR(10), DATEADD(day, 10, TimeAdded), 103) as 'Tenth_Day' 
from tblActiveList
)

Select 
    tenDays.DeviceID, 
    CONVERT(VARCHAR(10), RecordedDate, 103) as RecordedDate, 
    MAX(Angle) as Angle
from device_10thDay tenDays 
left JOIN tblActiveBladeLive tbl on tenDays.DeviceID = tbl.DeviceID and tenDays.Tenth_Day = CONVERT(VARCHAR(10), RecordedDate, 103) 
group by 
    tenDays.DeviceID, 
    CONVERT(VARCHAR(10), RecordedDate, 103)

答案 3 :(得分:0)

SELECT 
    DeviceID 
,   RecordedDate
,   Angle
FROM (
SELECT 
    tab.DeviceID
,   tab.RecordedDate
,   tab.Angle
,   ROW_NUMBER() OVER(PARTITION BY tab.DeviceID ORDER BY RecordedDate DESC) MaxRN  
FROM @tblActiveBladeLive tab
LEFT JOIN  @tblActive ta 
    ON ta.DeviceID = tab.DeviceID 
    AND ta.TimeAdded <= tab.RecordedDate 
    AND CAST(DATEADD(DAY, 10,CAST(TimeAdded AS DATE) ) AS DATETIME) + ' 23:59:59' >= tab.RecordedDate 
) D
WHERE 
    MaxRN = 1 

注意:

CAST(DATEADD(DAY, 10,CAST(TimeAdded AS DATE) ) AS DATETIME) + ' 23:59:59'

投放时间添加到日期并添加10天,然后给它23:59:59的时间,以确保我们将在10天获得所有记录。

您可以使用正确的日期更新tblActiveList表。这将使事情变得更有效,更易于长期使用。

答案 4 :(得分:0)

我不认为当同一日期有多个角度读数时,您接受的答案将解决问题。这是使用行编号的典型方法。

anotherSystem.actorSelection("/user/AnotherSupervisor/AnotherManagementReceiver").resolveOne() ! message