我在SQL Server 2012中有一个包含天气数据(每隔2分钟)的表
CREATE TABLE [dbo].[Test]
(
[SampleDateTime] [datetime] NULL,
[Unit ID] [nvarchar](4) NULL,
[WS Avg 2min] [float] NULL,
[WD Avg 2min] [float] NULL,
[WGS 10min] [float] NULL,
[WGD 10min] [float] NULL,
[Air Temp] [float] NULL,
[Rel Humidity] [float] NULL,
[Dew Point] [float] NULL,
[Pyranometer] [float] NULL,
[Quantum] [float] NULL,
[Air Pressure] [float] NULL,
[Snow Level] [float] NULL,
[Rainfall] [float] NULL,
[PW Current] [varchar](10) NULL,
[Visibility] [float] NULL,
[CBase 1] [float] NULL,
[CBase 2] [float] NULL,
[CBase 3] [float] NULL,
[Vert Vis] [float] NULL
) ON [PRIMARY]
我试图在一个月内获得每天的盛行风向。所以基本上是每天的方向计数,返回最大计数。 有了这个查询,我就接近了(来自MS Access)
SELECT
Day([SampleDateTime]) AS [Date],
Round([WD Avg 2min],0) AS WindDir,
Count(Round([WD Avg 2min],0)) AS [Count]
FROM
WeatherData
WHERE
(((Year([SampleDateTime]) * 12 + DatePart("m", [SampleDateTime])) = Year(Date()) * 12 + DatePart("m", Date()) - 0))
GROUP BY
Day([SampleDateTime]), Round([WD Avg 2min],0)
ORDER BY
Day([SampleDateTime]), Count(Round([WD Avg 2min],0)) DESC;
我得到了这个结果
Date WindDir Count
1 74 45
1 342 11
1 331 11
1 333 11
1 338 10
2 48 20
2 45 20
2 42 20
2 50 17
我似乎无法让查询返回相同的结果,但每个日期只有Max“Count”,如下所示
Date WindDir Count
1 74 45
2 48 20
么?
答案 0 :(得分:2)
我猜你的第一个查询是正确的,并希望减少到第二个结果。
使用CTE Using Common Table Expressions
test = serializers.CharField(required=False, read_only=True)