结合2个表的时间戳

时间:2015-03-19 20:35:57

标签: sql sql-server view

我正在尝试将两个具有相似时间戳但不相等的表组合在一起。 任何人都可以帮忙吗?

ChillerTable

+----------------+------------+-------------+
| t_stamp        | SupplyTemp | Return_temp |
|                |            |             |
| 3/5/2015 12:02 | 15         | 14          |
|                |            |             |
| 3/5/2015 12:45 | 10         | 9           |
|                |            |             |
| 3/5/2015 13:27 | 9          | 7           |
+----------------+------------+-------------+

+----------------+----------+-------------+
| t_stamp        | Weight   | Jacket_temp |
|                |          |             |
| 3/5/2015 12:15 | 1500     | 10          |
|                |          |             |
| 3/5/2015 13:02 | 1200     | 9           |
|                |          |             |
| 3/5/2015 13:14 | 900      | 8           |
+----------------+----------+-------------+

结果

+------------------+-------------+-------------+----------+-------------+
| Combined_t_stamp | Supply_temp | Return_temp | Weight   | Jacket_temp |
|                  |             |             |          |             |
| 3/5/2015 12:02   | 15          | 14          | NULL     | NULL        |
|                  |             |             |          |             |
| 3/5/2015 12:15   | NULL        | NULL        | 1500     | 10          |
|                  |             |             |          |             |
| 3/5/2015 12:45   | 10          | 9           | NULL     | NULL        |
|                  |             |             |          |             |
| 3/5/2015 13:02   | NULL        | NULL        | 1200     | 9           |
|                  |             |             |          |             |
| 3/5/2015 13:14   | NULL        | NULL        | 900      | 8           |
|                  |             |             |          |             |
| 3/5/2015 13:27   | 9           | 7           | NULL     | NULL        |
+------------------+-------------+-------------+----------+-------------+

我能做的最好的事情是:

 SELECT     dbo.ChillerTable.t_stamp AS Expr1, dbo.ChillerTable.*, dbo.Kettle32Table.*


    FROM         dbo.ChillerTable INNER JOIN
                          dbo.Kettle32Table ON dbo.ChillerTable.t_stamp = dbo.Kettle32Table.t_stamp


WHERE     (dbo.ChillerTable.t_stamp > CONVERT(DATETIME, '2015-03-19 00:00:00', 102))

但这只给了我时间戳上相等的行 当我选择!=时,它会给我更多行,但我希望两个表中的时间戳都在同一列中 任何正确方向的帮助将不胜感激。 我可能会在sql 2008中使用这个视图 谢,肯

3 个答案:

答案 0 :(得分:2)

SELECT COALESCE(t1.t_stamp, t2.t_stamp) t_stamp
      ,SupplyTemp
      ,Return_temp
      ,[Weight]
      ,Jacket_temp
FROM Table1 t1 FULL JOIN Table2 t2
ON t1.t_stamp = t2.t_stamp

<强>结果

╔═════════════════════════╦════════════╦═════════════╦════════╦═════════════╗
║         t_stamp         ║ SupplyTemp ║ Return_temp ║ Weight ║ Jacket_temp ║
╠═════════════════════════╬════════════╬═════════════╬════════╬═════════════╣
║ 2015-03-05 12:02:00.000 ║ 15         ║ 14          ║ NULL   ║ NULL        ║
║ 2015-03-05 12:15:00.000 ║ NULL       ║ NULL        ║ 1500   ║ 10          ║
║ 2015-03-05 12:45:00.000 ║ 10         ║ 9           ║ NULL   ║ NULL        ║
║ 2015-03-05 13:02:00.000 ║ NULL       ║ NULL        ║ 1200   ║ 9           ║
║ 2015-03-05 13:14:00.000 ║ NULL       ║ NULL        ║ 900    ║ 8           ║
║ 2015-03-05 13:27:00.000 ║ 9          ║ 7           ║ NULL   ║ NULL        ║
╚═════════════════════════╩════════════╩═════════════╩════════╩═════════════╝

答案 1 :(得分:0)

我不确定你想要什么,但DATEPART功能可以帮助你从两张桌子中找到类似的时间

INNER JOIN bo.Kettle32Table ON DATEPART(HOUR, dbo.ChillerTable.t_stamp) = DATEPART(HOUR, dbo.Kettle32Table.t_stamp)

答案 2 :(得分:0)

这里是查询,它将两个表中的数据合并为一个:

SELECT t_stamp, SupplyTemp, Return_temp, null as Weight, null as Jacket_temp
FROM dbo.ChillerTable
UNION ALL
SELECT t_stamp, null, null, Weight, Jacket_temp
FROM dbo.Kettle32Table

如果要合并具有相同时间戳的行,可以扩展它:

select t_stamp, max(SupplyTemp) as SupplyTemp, max(Return_temp) as Return_temp, max(Weight) as Weight, max(Jacket_temp) as Jacket_temp
from (
    SELECT t_stamp, SupplyTemp, Return_temp, null as Weight, null as Jacket_temp
    FROM dbo.ChillerTable
    UNION ALL
    SELECT t_stamp, null, null, Weight, Jacket_temp
    FROM dbo.Kettle32Table
) as a
group by t_stamp