【问题标题】:How to create a query on 2 existing tables and build a table(view) with data from both tables and a condition?如何在 2 个现有表上创建查询并使用来自表和条件的数据构建表(视图)?
【发布时间】:2015-06-23 05:27:46
【问题描述】:

抱歉,标题可能有点混乱。
我将尝试在这里完整解释并添加一些表格作为示例。
好的,我拥有的是一个 MS-SQL 数据库,用于存储来自某些车辆上安装的设备的数据/信息。
目前数据库中有2个表: “通信” - 一个大表,用于存储设备连接到TCP服务器时的所有信息。记录一个接一个地添加(此处仅插入)。
表格如下所示:

第二个表“EquipmentStatus”——它有 EquipmentID 作为主键,所以它是一个较小的表(这里只有 UPDATES),它看起来像这样:

好吧,我需要一个新表,其中包含在 Date_1 和 Date_2 之间进行通信的 Vehicles,即可以提供此功能的 SQL 查询

其中 Date_1 和 Date_2 分别在 6 月 22 日设置为 20)。

此外,下一步还将为在 Date_1 和 Date_2 之间没有通信的车辆获取一个表(视图),即您在下面看到的那个:

感谢您的时间和耐心!

【问题讨论】:

  • 您可以使用 sql-server 函数来表示您需要的两个表/视图,它可以采用两个参数(开始和结束日期)并为您提供动态结果。可以直接查询这两个函数,就像表格一样
  • 对不起,我忘了说最后一次通信的日期应该在给定的范围内 [Date_1 ... Date_2] 显然... 过失!

标签: sql sql-server


【解决方案1】:

我在没有数据的情况下尝试了这些,但这应该会让你走上正确的道路。

答:

SELECT DISTINCT s.Vehicle_Number, s.Vehicle_Status, s.DateLastCommunication
FROM EquipmentStatus s
INNER JOIN Communications c
ON s.Equipment_ID = c.Equipment_ID
WHERE c.DateTimeCommunication BETWEEN '2015-06-20' AND '2015-06-22'

乙:

SELECT DISTINCT s.Vehicle_Number, s.Vehicle_Status, s.DateLastCommunication
FROM EquipmentStatus s
INNER JOIN Communications c
ON s.Equipment_ID = c.Equipment_ID
WHERE s.Equipment_ID NOT IN 
    (SELECT Equipment_ID FROM Communications WHERE DateTimeCommunication
    BETWEEN '2015-06-20' AND '2015-06-22')

【讨论】:

    【解决方案2】:

    这就是你要找的吗?

    declare @startDate datetime, @enddate datetime
    
    --vehicles communicated
    select A.Vehicle_Number, b.Vehicle_Status, A.DataLastCommunication
    from (
    Select Vehicle_Number, MAX(DataLastCommunication) as DataLastCommunication
    from dbo.Communications
    where DataLastCommunication between @startDate and @enddate
    group by Vehicle_Number
    ) as A
      inner join dbo.EquipmentStatus b
          on a.Vehicle_Number=b.Vehicle_Number 
             and a.DataLastCommunication = b.DataLastCommunication    
    
    
    --vehicles did not communicate
    select a.Vehicle_Number, a.Vehicle_Status, a.DataLastCommunication
    from dbo.EquipmentStatus a
    where not exists(
      select 1 
      from dbo.Communications
      where Vehicle_Number=a.Vehicle_Number
        and DataLastCommunication between @startDate and @enddate
    )
    

    最后一个查询应该使用 Vehicles 表而不是通信表,因为可能有一辆新车在这两个表中的任何一个中都没有条目(这将返回车辆编号但状态和日期将为空)....

    【讨论】:

    • 我明天一定会试试这些(现在是凌晨 0:45 :)...我不知道 MAX() 的可用性 :) 希望它会起作用。谢谢,保持联系!
    • 当然...如果我做了错误的假设或误解了您的要求,请告诉我...如果是,我们可以随时解决...没有问题..干杯:-)跨度>
    【解决方案3】:

    在sql-server中,你可以使用函数返回两组不同的数据,就像两个新表一样:

    create function f_has_comm(@date1 datetime, @date2 datetime)
    returns table
    as
    return (select Vehicle_Number, Vehical_status, DateLastCommunication 
        from EquipmentStatus where EquipmentID in (select EquipmentID from Communication 
            where DatetimeCommunication>=@date1 and DatetimeCommunication<=@date2)
        )
    
    go
    
    create function f_no_comm(@date1 datetime, @date2 datetime)
    returns table
    as
    return (select Vehicle_Number, Vehical_status, DateLastCommunication 
        from EquipmentStatus where EquipmentID NOT IN (select EquipmentID from Communication 
            where DatetimeCommunication>=@date1 and DatetimeCommunication<=@date2)
        )
    
    go
    

    现在您可以从以下两个功能中进行选择:

     select * from f_has_comm('2015-6-1','2015-6-20');
     select * from f_no_comm('2015-6-1','2015-6-20');
    

    【讨论】:

      猜你喜欢
      • 2015-09-09
      • 2016-03-01
      • 2016-07-26
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2014-09-02
      • 1970-01-01
      相关资源
      最近更新 更多