嘗試:
SELECT TOP 3
a1.year,
a1.quarter,
a1.team,
sum(a1.price) as Total
FROM
tbl_sales a1
inner JOIN
tbl_sales a2
ON a1.year = a2.year
and a1.quarter = a2.quarter
and a1.team = a2.team
and a1.price = a2.price
where
some restrictions here
GROUP BY
a1.year,
a1.quarter,
a1.team
ORDER BY
sum(a1.price) DESC
編輯,請嘗試以下,你可能需要額外的標準,以確保您得到正確的結果集,但這應該返回爲where子句中指定的每個季度的前3名,我不知道爲什麼您使用的是自聯接,所以我離開了這一點,但它可以根據需要添加回:
SELECT TOP 3
a1.year,
a1.quarter,
a1.team,
sum(a1.price) as Total
FROM
tbl_sales a1
WHERE
'some restrictions here'
AND a1.quarter = 1 --(or however quarters are identified)
AND a1.year = 2015 --(or however years are identified)
GROUP BY
a1.year,
a1.quarter,
a1.team
ORDER BY
sum(a1.price) DESC
UNION ALL
SELECT TOP 3
a2.year,
a2.quarter,
a2.team,
sum(a2.price) as Total
FROM
tbl_sales a2
WHERE
'some restrictions here'
AND a2.quarter = 2
AND a2.year = 2015
GROUP BY
a2.year,
a2.quarter,
a2.team
ORDER BY
sum(a2.price) DESC
UNION ALL
SELECT TOP 3
a3.year,
a3.quarter,
a3.team,
sum(a3.price) as Total
FROM
tbl_sales a3
WHERE
'some restrictions here'
AND a3.quarter = 3
AND a3.year = 2015
GROUP BY
a3.year,
a3.quarter,
a3.team
ORDER BY
sum(a3.price) DESC
UNION ALL
SELECT TOP 3
a1.year,
a1.quarter,
a1.team,
sum(a1.price) as Total
FROM
tbl_sales a4
WHERE
'some restrictions here'
AND a4.quarter = 4
AND a4.year = 2015
GROUP BY
a4.year,
a4.quarter,
a4.team
ORDER BY
sum(a4.price) DESC
正如你可以看到有基本表之間沒有區別,除了where子句中,你不僅限於這4個表的聯合,它是隻是一個例子,但請記住,如果你需要覆蓋更多的宿舍,你需要添加更多的工會表。這可能會變得非常笨重,因爲它要求你隨着時間的推移不斷向表格添加表格。
最後加上'ORDER BY Total DESC'。 Access是否支持'SELECT TOP 3'? – jarlh
https://support.office.com/zh-cn/article/ALL-DISTINCT-DISTINCTROW-TOP-Predicates-24f2a47d-a803-4c7c-8e81-756fe298ce57表示TOP完全有效。 – ThisIsImpossible