简体   繁体   English

合并临时表和选择查询

[英]Merging a temp table with select query

I want to add the TempTable column 'Rad' into the select query shown below: 我想将TempTable列“ Rad”添加到如下所示的选择查询中:

Table : TempTable 表格:临时表格

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

Table : Daily 表格:每日

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

Table : Expected Result 表:预期结果

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

with this query, 通过此查询,

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.]

The problem is that the query result is not 'matching' or synchronized correctly with both tables. 问题在于查询结果未与两个表“匹配”或正确同步。 Thus, is throwing me 14 rows instead of 6. 因此,给我扔了14行而不是6行。

Current Result: 当前结果:

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

I am going to guess that you are including the time field in your query. 我猜您在查询中包括了time字段。 I used the following query and returned 6 records, but if I include the time I get more: 我使用以下查询并返回了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])

See SQL Fiddle with demo 参见带有演示的SQL Fiddle

You need to group by DAY(Daily.[Date]) : 您需要按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.] 
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.]

Something like that is what you want. 那样的东西就是您想要的。 You're going to have problems if you use the day(date) function, since that just returns the day of the month. 如果使用day(date)函数,将会遇到问题,因为那只会返回月份中的某天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM