2016-08-11 68 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 
       ); 

您可能需要限制都增加了外部查询和内部查询,这取决于它们是什么。