0

我想将 TempTable 列“Rad”添加到如下所示的选择查询中:

表:临时表

Date        Rad.
----------------------
2012-08-09  610.345802
2012-08-10  589.090580
2012-08-11  525.193952
2012-08-12  518.484953
2012-08-13  251.790038
2012-08-14  461.892979

表:每日

Date        Time      Precipitation   WindChill  DewPoint
----------------------------------------------------------
2012-08-09  00:00:00  0.000273        27.844708  19.1826
2012-08-09  00:10:00  0.697821        25.311829  19.2645
2012-08-09  00:20:00  2.097455        27.968444  19.4142
2012-08-09  00:30:00  1.033763        25.705078  19.3842
2012-08-09  00:40:00  1.008760        26.343726  19.4563
2012-08-09  00:50:00  0.973456        26.869532  19.5293
.
.
.        

表:预期结果

Date        Rad.        Precipitation  WindChill  DewPoint
----------------------------------------------------------
2012-08-09  610.345802  0.005789       28.903764  19.9802
2012-08-10  589.090580  0.763590       27.903685  19.7265
2012-08-11  525.193952  0.998734       24.574824  19.4351
2012-08-12  518.484953  0.334567       29.909372  20.4865
2012-08-13  251.790038  0.789032       27.902474  18.7653
2012-08-14  461.892979  0.123567       26.987688  18.9876

有了这个查询,

SELECT Daily.[Date],    
       AVG(Daily.[Precipitation]) as [Precipitation],

       ##TempTable.[Rad.],

       AVG(Daily.[Wind Chill]) as [Wind Chill], 
       AVG(Daily.[Dew Point]) as [Dew Point]
FROM Daily
INNER JOIN ##TempTable
ON DAY(Daily.[Date]) = DAY(##TempTable.[Date])
GROUP BY Daily.[Date], ##TempTable.[Rad.]

问题是查询结果不是“匹配”或与两个表正确同步。因此,我扔了 14 行而不是 6 行。

当前结果:

Date    Rad.            Date            Precipitation   Wind Chill  Dew Point
------------------------------------------------------------------------------------
2012-08-18  541.917573  2012-08-18  0.000000    1.460818    NULL
2012-08-12  518.484953  2012-08-12  0.000273    1.854153    NULL
2012-08-17  327.291210  2011-08-17  0.000000    0.000000    18.453193
2012-08-15  428.649430  2012-08-15  0.000536    1.170602    NULL
2012-08-09  610.345802  2012-08-09  0.000014    1.008784    NULL
2012-08-10  589.090580  2012-08-10  0.000000    1.402685    NULL
2012-08-13  251.790038  2012-08-13  0.000882    0.691585    NULL
2012-08-18  541.917573  2011-08-18  0.000000    0.000006    17.041953
2012-08-16  536.200291  2012-08-16  0.000028    1.120068    NULL
2012-08-19  597.631053  2011-08-19  0.000000    0.000006    16.056780
2012-08-19  597.631053  2012-08-19  0.000000    1.565405    NULL
2012-08-14  461.892979  2012-08-14  0.000390    1.196409    NULL
2012-08-17  327.291210  2012-08-17  0.001301    0.886275    NULL
2012-08-11  525.193952  2012-08-11  0.000000    2.076477    NULL
4

3 回答 3

1

我猜您time在查询中包含该字段。我使用了以下查询并返回了 6 条记录,但如果我包含了time我会得到更多:

select *
from temp t
inner join
(
  select dt,
    AVG([Precip]) as [Precipitation],
    AVG([Wind]) as [Wind Chill], 
    AVG([Dew]) as [Dew Point]
  from daily
  group by dt
) d
  on DAY(t.[Dt]) = DAY(d.[Dt])

请参阅带有演示的 SQL Fiddle

于 2012-08-23T04:49:52.053 回答
0

您需要按以下方式分组DAY(Daily.[Date])

SELECT DAY(Daily.[Date]),     
       AVG(Daily.[Precipitation]) as [Precipitation], 

       ##TempTable.[Rad.], 

       AVG(Daily.[Wind Chill]) as [Wind Chill],  
       AVG(Daily.[Dew Point]) as [Dew Point] 
FROM Daily 
INNER JOIN ##TempTable 
ON DAY(Daily.[Date]) = DAY(##TempTable.[Date]) 
GROUP BY DAY(Daily.[Date]), ##TempTable.[Rad.] 
于 2012-08-21T16:31:52.377 回答
0
SELECT DATEADD(DD, DATEDIFF(DD,0,Daily.[Date]),0) Date,    
       AVG(Daily.[Precipitation]) as [Precipitation],

       ##TempTable.[Rad.],

       AVG(Daily.[Wind Chill]) as [Wind Chill], 
       AVG(Daily.[Dew Point]) as [Dew Point]
FROM Daily
INNER JOIN ##TempTable
ON DATEADD(DD, DATEDIFF(DD,0,Daily.[Date]),0) = ##TempTable.[Date]
GROUP BY DATEADD(DD, DATEDIFF(DD,0,Daily.[Date]),0), ##TempTable.[Rad.]

类似的东西就是你想要的。如果你使用这个day(date)函数,你会遇到问题,因为它只返回月份的日期。

于 2012-08-21T19:26:49.257 回答