请原谅我,因为我确定我缺少一些简单的分组查询或子查询,但我可以解决以下问题并需要一些帮助。
我在表中有数据,需要从 PointName 返回不同的值,其中的平均值AdjEasting, AdjNorthing & AdjHeight
(这是存在 PointName 值的每一列的前 4 个返回值的平均值)
到目前为止,我有这些数据:
PointName,AdjEasting,AdjNorthing,AdjHeight
51,76672.47700,36190.37870,128.54820
51,76672.47800,36190.38060,128.55070
51,76672.47680,36190.38010,128.55020
51,76672.47640,36190.37910,128.55020
51,76672.47690,36190.37930,128.54990
51,76672.47740,36190.38050,128.54990
52,76781.27600,36069.18870,127.80420
52,76781.27580,36069.18800,127.80350
52,76781.27580,36069.18930,127.80310
52,76781.27630,36069.18900,127.80370
52,76781.27630,36069.18870,127.80330
53,76905.20930,35932.21400,126.79920
53,76905.20910,35932.21290,126.79970
53,76905.20550,35932.21180,126.79950
53,76905.20820,35932.21350,126.80080
53,76905.20910,35932.21380,126.79920
53,76905.20830,35932.21370,126.80160
55,77050.28410,36001.64670,128.73920
55,77050.28400,36001.64790,128.73990
55,77050.28440,36001.64590,128.74010
55,77050.28400,36001.64650,128.74010
55,77050.28360,36001.64590,128.74010
55,77050.28500,36001.64780,128.73990
55,77050.28630,36001.64760,128.74200,
55,77050.28430,36001.64630,128.73960
...我想要这些数据:
51,76672.47705,36190.37963,128.54983
52,76781.27598,36069.18875,127.80363
53,76905.20803,35932.21305,126.79980
55,77050.28413,36001.64675,128.73983
这是我的查询,但它返回一个不同的 ID,但 ID 中的每条记录的平均值相同:
SELECT DISTINCT
PointName
,(SELECT avg(AvEasting)
FROM
(SELECT TOP 4
cast(AdjEasting as float) as AvEasting
FROM AdjFile
ORDER BY PointName, epoch desc) AdjFile)
,(SELECT avg(AvNorthing)
FROM
(SELECT TOP 4
cast(AdjNorthing as float) as AvNorthing
FROM AdjFile
ORDER BY PointName, epoch desc) AdjFile)
,(SELECT avg(AvHeight)
FROM
(SELECT TOP 4
cast(AdjHeight as float) as AvHeight
FROM AdjFile
ORDER BY PointName,epoch desc) AdjFile)
FROM AdjFile</P>
编辑
我可以使用以下代码获取要返回的单个值:
SELECT
PointName
,avg(AvEasting)
,avg(AvNorthing)
,avg(AvHeight)
FROM
(SELECT TOP 4
PointName
,cast(AdjEasting as float) as AvEasting
,cast(AdjNorthing as float) as AvNorthing
,cast(AdjHeight as float) as AvHeight
FROM AdjFile
ORDER BY PointName,epoch desc) AdjFile
GROUP BY PointName
ORDER BY PointName