每10分钟生成一次报告

时间:2011-05-30 09:45:47

标签: sql-server sql-server-2005

我有两个表温度和压力,读数如下图所示,我的第一个表每10分钟生成一个值

第一个表包含以下数据:

Temperature          Time             Date 
28.8      1900-01-01 13:14:54.000   2011-05-25 00:00:00
29.1      1900-01-01 13:15:04.000   2011-05-25 00:00:00
29.4      1900-01-01 13:15:14.000   2011-05-25 00:00:00
29.7      1900-01-01 13:15:24.000   2011-05-25 00:00:00
30        1900-01-01 13:15:34.000   2011-05-25 00:00:00
30.3      1900-01-01 13:15:44.000   2011-05-25 00:00:00
30.6      1900-01-01 13:15:54.000   2011-05-25 00:00:00
30.9      1900-01-01 13:16:04.000   2011-05-25 00:00:00
31.2      1900-01-01 13:16:14.000   2011-05-25 00:00:00

我的第二个表每20分钟生成一个值,我将这个值存储在数据库中,即Sql Server

第二个表包含

Pressure         Time                  Date
0.9 1900-01-01 13:14:04.000 2011-05-25 00:00:00
1.7 1900-01-01 13:14:24.000 2011-05-25 00:00:00
2.5 1900-01-01 13:14:44.000 2011-05-25 00:00:00
3.3 1900-01-01 13:15:04.000 2011-05-25 00:00:00
4.1 1900-01-01 13:15:24.000 2011-05-25 00:00:00
4.9 1900-01-01 13:15:44.000 2011-05-25 00:00:00
5.7 1900-01-01 13:16:04.000 2011-05-25 00:00:00
6.5 1900-01-01 13:16:24.000 2011-05-25 00:00:00
7.3 1900-01-01 13:16:44.000 2011-05-25 00:00:00

我希望我的输出返回:

Date          Time       Temperature   Pressure
25/05/2011   13:14:54     28.8           0
25/05/2011   13:15:04     29.1           3.3
25/05/2011   13:15:14     29.4           0
25/05/2011   13:15:24     29.7           4.1

依此类推,用户将在该日期选择特定日期,那里应该显示哪些读数,例如,如果他选择25/05/2011应该显示读数。我必须使用C#将此结果绑定到gridview。

1 个答案:

答案 0 :(得分:2)

如果您不介意丢失日期时间记录的可能缺口,FULL OUTER JOIN就足够了。

SELECT  ISNULL(t1.Date, t2.Date)
        , ISNULL(t1.Time, t2.Time)
        , ISNULL(t1.Temperature, 0)
        , ISNULL(t2.Pressure, 0)
FROM    Table1 t1
        FULL OUTER JOIN Table2 t2 ON t2.Date = t1.Date AND t2.Time = t1.Time