2016-08-11 54 views
1

我有一個列,如年,季度,團隊和價格的表。它顯示了團隊的每一筆銷售。我們在該公司有10個團隊。 我想知道每年的每個季度,哪三個團隊銷售最全面。那麼我卡在哪裏?在這一點上,我將每個團隊每季度的總銷售額分組。仍然缺少只有最高銷售額的三個最佳團隊。我得到這個至今:MS Access 2010 SQL前3集團銷售

SELECT 
    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 

東西告訴我,我很接近,這只是一個頂子查詢功能將幫助,但我無法弄清楚。任何幫助非常感謝:) 非常感謝!

+0

最後加上'ORDER BY Total DESC'。 Access是否支持'SELECT TOP 3'? – jarlh

+0

https://support.office.com/zh-cn/article/ALL-DISTINCT-DISTINCTROW-TOP-Predicates-24f2a47d-a803-4c7c-8e81-756fe298ce57表示TOP完全有效。 – ThisIsImpossible

回答

0

嘗試:

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個表的聯合,它是隻是一個例子,但請記住,如果你需要覆蓋更多的宿舍,你需要添加更多的工會表。這可能會變得非常笨重,因爲它要求你隨着時間的推移不斷向表格添加表格。

+0

嗨。感謝您抽出寶貴的時間!!這給了我所有季度每年最高的團隊銷售額。我也需要每個季度以及....更接近雖然:) – luftlinie

+0

爲了做到這一點,你可能需要爲每個季度創建一個單獨的表,然後將它們結合在一起,這將每季度與前3隊一起追加到結果集並給你你正在尋找的東西。 – ThisIsImpossible

+0

任何快速的想法如何做到這一點? – luftlinie

0

在MS Access,您可以使用INTOP做到這一點:

select s.* 
from tbl_sales as s 
where s.team in (select top (3) s2.team 
       from tbl_sales as s2 
       where s2.year = s.year and s2.quarter = s.quarter 
       order by s2.price desc 
       ); 

您可能需要限制都增加了外部查詢和內部查詢,這取決於它們是什麼。