繁体   English   中英

对多个表的 SQL 查询以获取最新数据

[英]SQL query to multiple tables for the most recent data

我目前的 SQL 查询是:

select 
    t.ZoneNumber, t.Mode, t.DeviceA, t.DeviceB, t.LevelA, t.LevelB,
    t.Counter, t.Options, t.AzureDeviceID_Field_Device, t.AzureDeviceID,
    t.TimeStamp,
    tt.ZoneName, tt.AzureDeviceID_Field_Device, tt.ZoneNumber, tt.TimeStamp 
from 
    [dbo].[tblZone] t, [dbo].[tblZoneName] tt   
inner join 
    (select 
         ZoneNumber, max(TimeStamp) as MaxDate 
     from
         [dbo].[tblZone] 
     where 
         AzureDeviceID_Field_Device like 'myFirstDevicea' 
     group by 
         ZoneNumber) tm on t.ZoneNumber = tm.ZoneNumber and t.TimeStamp = tm.MaxDate 

我想要做的是同时查询两个或多个表,以根据 DeviceID(即 AzureDeviceID_Field_Device 和 Zonenumber)获取每个表上的最新记录。

对于我的示例,我的 DeviceID 也是一个 KEY,它是“myFirstDevicea”。 它将生成多个区域的记录,每个区域都有一个数字,例如 1 到 10。对于每个区域编号,我只想生成最新的。

tblZone 具有区域参数,并且 tbl zonename 作为区域的名称。 此名称可能会不时更改。

所以我的目标是从 tablezone 生成一个最近的列表,我可以这样做,然后从 table zonename 中获取最新的名称。

当我运行查询时,我收到各种无法绑定的错误?

有人可以帮我解决这个查询。

表区

表区名称

  select t.ZoneNumber,t.Mode,t.DeviceA,t.DeviceB,t.LevelA,t.LevelB,t.Counter,t.Options,t.AzureDeviceID_Field_Device,t.AzureDeviceID,t.TimeStamp
  from [dbo].[tblZone] t
  inner join (select ZoneNumber, max(TimeStamp) as MaxDate FROM [dbo].[tblZone] WHERE AzureDeviceID_Field_Device LIKE 'myFirstDevicea' group by ZoneNumber )tm 
  on  t.ZoneNumber = tm.ZoneNumber and t.TimeStamp = tm.MaxDate 

  select tt.ZoneName,tt.AzureDeviceID_Field_Device,tt.ZoneNumber,tt.TimeStamp
  from  [dbo].[tblZoneName] tt 
  inner join (select ZoneNumber, max(TimeStamp) as MaxDate FROM [dbo].[tblZoneName] WHERE AzureDeviceID_Field_Device LIKE 'myFirstDevicea' group by ZoneNumber )tm 
  on  tt.ZoneNumber = tm.ZoneNumber and tt.TimeStamp = tm.MaxDate 

您可以通过在公共表表达式中使用派生表来实现这一点,利用窗口函数来识别两个表中的最新TimeStamp值:

-- Built test data:
declare @tblZone table(AzureDeviceID_Field_Device nvarchar(100),Zonenumber int, [TimeStamp] datetime2);
declare @tableZoneName table(AzureDeviceID_Field_Device nvarchar(100),ZoneNumber int, ZoneName nvarchar(100), [TimeStamp] datetime2);
insert into @tblZone values ('a1',1 ,getdate()-2),('a1',1 ,getdate()-1),('a1',1 ,getdate()),('a1',15,getdate()-4),('a1',15,getdate()-3),('a1',15,getdate()-2);
insert into @tableZoneName values ('a1',1 ,'Zone A1',getdate()-2),('a1',1 ,'Zone A1',getdate()-1),('a1',1 ,'Zone A1',getdate()),('a1',15,'Zone A15',getdate()-4),('a1',15,'Zone A15',getdate()-3),('a1',15,'Zone A15',getdate()-2);

-- Common Table Expressions (CTE) to add a Row Number to both tables:
with z as
(
    select AzureDeviceID_Field_Device
            ,Zonenumber
            ,[TimeStamp]    -- row_number() returns the order the rows are in, grouped by the PARTITION columns and ordered by the ORDER BY colmns
            ,row_number() over (partition by AzureDeviceID_Field_Device, ZoneNumber
                                order by [TimeStamp] desc
                               ) as rn
    from @tblZone
),zn as
(
    select AzureDeviceID_Field_Device
            ,Zonenumber
            ,ZoneName
            ,[TimeStamp]
            ,row_number() over (partition by AzureDeviceID_Field_Device, ZoneNumber
                                order by [TimeStamp] desc
                               ) as rn
    from @tableZoneName
)
-- Join the two derived tables together to get your result:
select *
from z
    left join zn
        on z.AzureDeviceID_Field_Device = zn.AzureDeviceID_Field_Device
            and z.ZoneNumber = zn.Zonenumber
            and zn.rn = 1    -- Rows with a rn value of one will have the most recent TimeStamp.
where z.rn = 1
order by z.AzureDeviceID_Field_Device
        ,z.ZoneNumber;

输出:

+----------------------------+------------+-----------------------------+----+----------------------------+------------+----------+-----------------------------+----+
| AzureDeviceID_Field_Device | Zonenumber |          TimeStamp          | rn | AzureDeviceID_Field_Device | Zonenumber | ZoneName |          TimeStamp          | rn |
+----------------------------+------------+-----------------------------+----+----------------------------+------------+----------+-----------------------------+----+
| a1                         |          1 | 2017-10-23 09:33:43.3066667 |  1 | a1                         |          1 | Zone A1  | 2017-10-23 09:33:43.3233333 |  1 |
| a1                         |         15 | 2017-10-21 09:33:43.3066667 |  1 | a1                         |         15 | Zone A15 | 2017-10-21 09:33:43.3233333 |  1 |
+----------------------------+------------+-----------------------------+----+----------------------------+------------+----------+-----------------------------+----+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM