在SQL Server中,我具有以下表结构
alert_details_status 表:
alertTypeId macId timeStamp is_notified escalation_status alertType
123 13446 1547722123000 true completed 408
alert_details 表:
basicid alertType alertTypeId alertDescription macId timeStamp companyId alertName alertCondition unitType channelType alertValue expectedValue
1234 406 123 testalert 13446 1547722123000 1234 test alert name testalert Centimeters length 50 60
1295 409 127 testalert 13448 1547722123000 1234 test alert name testalert Centimeters length 50.2 60.3
1298 409 128 testalert 13448 1547722123000 1234 test alert name testalert Centimeters length 50.2 60.3
1238 408 123 testalert 13446 1547722123000 1234 test alert name testalert Centimeters length 50.2 60.3
1255 409 128 testalert 13448 1548135899000 1234 test alert name testalert Centimeters length 50.2 60.3
1256 409 128 testalert 13448 1548135899000 1234 test alert name testalert Centimeters length 50.2 60.3
1251 408 123 testalert 13446 1548148705000 1234 test alert name testalert Centimeters length 50.2 60.3
如果alert_details_status
表记录的每个timestamp >30 minutes(compared to present time)
,alertTypeId
,macId
组中的alertType
为is_notified
,并且{{ 1}}完成,然后应选择具有每条记录最大时间戳的相应escalation_status
,alertTypeId
,macId
(即使组的时间戳相同,每组最多一条记录)。>
如何获得上述结果。
我尝试使用以下查询,但未返回任何内容。实际上,它应返回alertType
中的1251基本ID记录。
alert_details_status table having timestamp >30 min compared to present timestamp. So for that alertTypeId ,macId alertType it should return maximum of timestamp for a group having alertTypeId ,macId alertType(i,e 1251)
我想要的结果是:
SELECT *
FROM (SELECT t1.*,
Row_number()
OVER(
partition BY t1.alerttype, t1.alerttypeid, t1.macid
ORDER BY t1.timestamp DESC) rn
FROM [test].[dbo].[alertdetails] as t1
RIGHT JOIN [test].[dbo].[alert_details_status] t2
ON t1.macid = t2.macid
AND t1.alerttypeid = t2.alerttypeid
AND t1.alerttype = t2.alerttype
AND t2.is_notified = 'true '
AND t2.escalation_status = 'completed'
WHERE ((DATEDIFF(s, '1970-01-01', GETUTCDATE())-(t2.timeStamp/1000))/60)>4400 and t2.alerttypeid IS NULL ) t
WHERE t.rn = 1