2013-03-13 51 views
0

我想獲得在特定年份的特定月份中銷量最高的十大產品。我做了以下:分組時的限制數記錄

SELECT Year(date1), Month(date1), `ProductID`, sum(`Price`*`Quantity`)"Sales" 
FROM `products` 
Group By Year(date1), Month(date1), ProductId 
order by Year(date1), Month(date1), Sales Desc; 

但是,它給了我所有的產品,我只想要十大產品。 無法理解到的10條記錄申請限制,

+0

我認爲你將有一個問題,這個問題,因爲你是年/月/的productId上市條款和使用。一條直線的「限制」將會給你排名前10位的年/月/ productID組合,而不僅僅是前10位的產品 – 2013-03-13 19:10:21

回答

1

如果你排序是設置爲你想要的,只是添加

LIMIT 0,10 

到您的查詢

0

使用MySQL內置的功能限制在您選擇查詢

LIMIT {[offset,] row_count | row_count OFFSET offset} 
      ^    ^
     Starting tuple  Number of tuples 

年底欲瞭解更多閱讀Go Here

0

考慮,其中by子句重組組,秩序DATE_FORMAT

SELECT date_format(date1) as month, `ProductID`, sum(`Price`*`Quantity`)"Sales" 
FROM `products` 
WHERE date1 > '2013-01-01' and date1 < '2013-02-01' 
Group By ProductId 
order by Sales Desc 
Limit 0,10 
;