Last active
June 17, 2016 09:40
-
-
Save freeart/fb9b77cb8c0daa3a5c160bf1a90921c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * FROM ( | |
SELECT ROW_NUMBER() OVER (ORDER BY data.[DeviceId]) as ID, data.[UserId], data.[DeviceId], | |
l2u.[PurchaseTime], lic.[Name] [LicenceName], | |
d.[Name] [DeviceName], d.[Code], d.[Number], d.[VersionHW], sb.[Version] [VersionSW], d.[DeviceStatus], d.[SigFoxActive], d.[Comment], | |
CASE WHEN s.id > sb.id THEN s.[BatteryPercent] ELSE sb.[BatteryPercent] END AS BatteryPercent, | |
CASE WHEN nog.[Latitude] IS NULL OR nog.[Latitude] = 0 THEN CASE WHEN l.[Latitude] IS NULL OR l.[Latitude] = 0 THEN CASE WHEN e.[Latitude] IS NULL OR e.[Latitude] = 0 THEN 0 ELSE e.[Latitude] END ELSE l.[Latitude] END ELSE nog.[Latitude] END AS Latitude, | |
CASE WHEN nog.[Longitude] IS NULL OR nog.[Longitude] = 0 THEN CASE WHEN l.[Longitude] IS NULL OR l.[Longitude] = 0 THEN CASE WHEN e.[Longitude] IS NULL OR e.[Longitude] = 0 THEN 0 ELSE e.[Longitude] END ELSE l.[Longitude] END ELSE nog.[Longitude] END AS Longitude, | |
CASE WHEN nog.[Precision] IS NULL OR nog.[Precision] = 0 THEN CASE WHEN l.[Precision] IS NULL OR l.[Precision] = 0 THEN CASE WHEN e.[Precision] IS NULL OR e.[Precision] = 0 THEN 0 ELSE e.[Precision] END ELSE l.[Precision] END ELSE nog.[Precision] END AS Precision, | |
n.[NotifiedAt] | |
FROM ( | |
SELECT data.[UserId], data.[DeviceId], s.[StatusId], sb.[StandbyId], pon.[PowerONId], poff.[PowerOFFId], nog.[NoGPSId], l.[LocationId], e.[EndOfMovementId], l2u.[PurchaseId], n.[NotificationId] FROM ( | |
SELECT u.[Id] [UserId], d.[Id] [DeviceId] | |
FROM [dbo].[Devices] d | |
left outer join [dbo].[AbpUsers] u on u.id = d.[UserId] | |
) data | |
left outer join (select MAX(c.[Id]) as [StatusId], p.[DeviceId] from [dbo].[StatusNotifications] c inner join [dbo].[Notifications] p on p.[Id] = c.[Id] group by p.[DeviceId]) s on s.[DeviceId] = data.[DeviceId] | |
left outer join (select MAX(c.[Id]) as [StandbyId], p.[DeviceId] from [dbo].[StandbyNotifications] c inner join [dbo].[Notifications] p on p.[Id] = c.[Id] group by p.[DeviceId]) sb on sb.[DeviceId] = data.[DeviceId] | |
left outer join (select MAX(c.[Id]) as [PowerONId], p.[DeviceId] from [dbo].[PowerONNotifications] c inner join [dbo].[Notifications] p on p.[Id] = c.[Id] group by p.[DeviceId]) pon on pon.[DeviceId] = data.[DeviceId] | |
left outer join (select MAX(c.[Id]) as [PowerOFFId], p.[DeviceId] from [dbo].[PowerOFFNotifications] c inner join [dbo].[Notifications] p on p.[Id] = c.[Id] group by p.[DeviceId]) poff on poff.[DeviceId] = data.[DeviceId] | |
left outer join (select MAX(c.[Id]) as [NoGPSId], p.[DeviceId] from [dbo].[NoGPSNotifications] c inner join [dbo].[Notifications] p on p.[Id] = c.[Id] group by p.[DeviceId]) nog on nog.[DeviceId] = data.[DeviceId] | |
left outer join (select MAX(c.[Id]) as [LocationId], p.[DeviceId] from [dbo].[LocationNotifications] c inner join [dbo].[Notifications] p on p.[Id] = c.[Id] group by p.[DeviceId]) l on l.[DeviceId] = data.[DeviceId] | |
left outer join (select MAX(c.[Id]) as [EndOfMovementId], p.[DeviceId] from [dbo].[EndOfMovementNotifications] c inner join [dbo].[Notifications] p on p.[Id] = c.[Id] group by p.[DeviceId]) e on e.[DeviceId] = data.[DeviceId] | |
left outer join (select MAX(p.[Id]) as [PurchaseId], p.[DeviceId] from [dbo].[Purchases] p group by p.[DeviceId]) l2u on l2u.[DeviceId] = data.[DeviceId] | |
left outer join (select MAX([Id]) as [NotificationId], [DeviceId] from [dbo].[Notifications] group by [DeviceId]) n on n.[DeviceId] = data.[DeviceId] | |
) data | |
left outer join [dbo].[StatusNotifications] s on s.id = data.[StatusId] | |
left outer join [dbo].[StandbyNotifications] sb on sb.id = data.[StandbyId] | |
left outer join [dbo].[PowerONNotifications] pon on pon.id = data.[PowerONId] | |
left outer join [dbo].[PowerOFFNotifications] poff on poff.id = data.[PowerOFFId] | |
left outer join [dbo].[NoGPSNotifications] nog on nog.id = data.[NoGPSId] | |
left outer join [dbo].[LocationNotifications] l on l.id = data.[LocationId] | |
left outer join [dbo].[EndOfMovementNotifications] e on e.id = data.[EndOfMovementId] | |
inner join [dbo].[Devices] d on d.[Id] = data.[DeviceId] | |
left outer join [dbo].[Purchases] l2u on l2u.id = data.[PurchaseId] | |
left outer join [dbo].[Licences] lic on lic.id = l2u.[LicenceId] | |
left outer join [dbo].[Notifications] n on n.id = data.[NotificationId] | |
) a | |
WHERE a.ID > 1 and a.ID <= 1 + 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment