I want to add the TempTable column 'Rad' into the select query shown below:
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.
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. I used the following query and returned 6 records, but if I include the time
I get more:
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])
You need to group by 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.