Sql 组合来自2个表的时间戳

Sql 组合来自2个表的时间戳,sql,sql-server,view,Sql,Sql Server,View,我试图将两个时间戳相似但不相等的表组合在一起。 有人能帮忙吗 冷却台 +----------------+------------+-------------+ | t_stamp | SupplyTemp | Return_temp | | | | | | 3/5/2015 12:02 | 15 | 14 | | | |

我试图将两个时间戳相似但不相等的表组合在一起。 有人能帮忙吗

冷却台

+----------------+------------+-------------+
| 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           |
+----------------+------------+-------------+

结果

+------------------+-------------+-------------+----------+-------------+
| 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中使用此视图

Thx,Ken

我不确定您到底想要什么,但是DATEPART函数应该可以帮助您从两个表中查找相似的时间

INNER JOIN bo.Kettle32Table ON DATEPART(HOUR, dbo.ChillerTable.t_stamp) = DATEPART(HOUR, dbo.Kettle32Table.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        ║
╚═════════════════════════╩════════════╩═════════════╩════════╩═════════════╝

以下是将两个表中的数据合并为一个表的查询:

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

除非将两个值作为字符串组合在一起,否则不能将它们放入一列中。根本不清楚你想在这里做什么。我质疑您的代码,因为您使用datetime作为两个表之间的键。这要么表明表结构非常糟糕,要么缺乏对连接工作方式的理解。可能两者都有。我想合并t_stamp列,并让其他字段自己显示。我会尝试的,谢谢!
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