【发布时间】:2016-09-03 19:14:25
【问题描述】:
问题是“列出每年销量前三的产品”
我执行了以下查询
select top 3 b.CalendarYear,c.ProductKey, d.EnglishProductSubcategoryName ,SUM(a.SalesAmount) as SALES
from FactInternetSales as A inner join dimdate as B
on a.OrderDateKey =b.DateKey
inner join DimProduct as c
on c.ProductKey = a.ProductKey
inner join DimProductSubcategory as d
on c.ProductSubcategoryKey = d.ProductSubcategoryKey
inner join DimProductCategory as e
on d.ProductCategoryKey=e.ProductCategoryKey
group by b.CalendarYear,c.ProductKey, d.EnglishProductSubcategoryName
order by SALES desc
我得到了以下答案
CalendarYear ProductKey EnglishProductSubcategoryName SALES
2006 312 Road Bikes 658401.68
2006 313 Road Bikes 608305.90
2006 310 Road Bikes 608305.90
我的问题是为什么只有“2006 年”的数据,为什么不是所有年份?
【问题讨论】:
-
不能是 [mySQL]。 MySQL 不使用
TOP n语法。 -
您确定您的标签正确吗?
select top 3不是mysql语法 -
公路自行车在 2006 年很火。
-
我不确定你是否可以使用 top 关键字。如果可以,那么查询的结果是正确的,因为它只给你 3 条记录。这样一来,您就不能为每个组大喊 3 条记录!让我们尝试另一种方式
-
它的 SQL 服务器不是我的 sql
标签: sql sql-server greatest-n-per-group