修改select语句

时间:2017-01-25 07:04:25

标签: sql sql-server tsql

我正在尝试修改此select语句

getPem

仅在今天的日期生成平均值低。然后将它添加到名为TodayWeather的新表中。我将如何完成这项工作?

3 个答案:

答案 0 :(得分:0)

您可以从一个表中复制数据并插入另一个表中,如下所示: 例如,你的新表是TodayWeather,并且列有' AvgLo' http://www.w3schools.com/sql/sql_insert_into_select.asp

我正在为单列做这个,  你的餐桌'天气' donot有日期字段,如果你添加日期,那么查询看起来像这样

Insert into TodayWeather ( AvgLo ) SELECT AvgLo as 'Average Low'  FROM dbo.Weather where date = 'some date'

答案 1 :(得分:0)

您可以尝试这样:

INSERT INTO TodaysWeather
SELECT [Month], [Day], AVG(AvgLo) as [Average Low] 
FROM dbo.Weather 
WHERE [Month] = MONTH(GETDATE()) AND [Day] = DAY(GETDATE())
GROUP BY Month, Day

答案 2 :(得分:0)

CREATE VIEW TodaysWeather
SELECT [Month]
      ,[Day]
      ,AvgLo 'Average Low' 
FROM dbo.Weather 
WHERE [Month] = MONTH(GETDATE()) 
AND [Day] = DAY(GETDATE())

这将始终是最新的:)