Sql如何计算和排列数据顺序

时间:2015-03-27 11:20:07

标签: php mysql sql

我有一张桌子销售
两个Colums

_______________________
Store_name | Sale_Item |
_______________________ 
abc Store      |  Xyz Candy|
abc Store      |  Xyz Candy|
abc Store      |  Xyz Candy|
abc Store      |  Xyz Candy|
gfh Store      |  Xyz Candy|
abc Store      |  Xyz Candy|
gfh Store      |  Xyz Candy|
gfh Store      |  Xyz Candy|
gfh Store      |  Xyz Candy|
gfh Store      |  Xyz Candy|
gfh Store      |  Xyz Candy|

我如何计算商店销售和排序按销售排序 例如 结果

_______________________
Store_name | Sale_Item |
------------------------
gfh Store       |  6    |
abc Store       |  5    |

参见gfh计算的销售额和价值是6所以Gfh是最高的 Abc是最低销售额和计算价值是5因此它的下降 基本上我的问题是如何统计和安排数据顺序

6 个答案:

答案 0 :(得分:2)

Select store_name,count(store_name) as number_of_stores 
from sale 
group by store_name 
order by count(store_name) desc;

答案 1 :(得分:1)

<强>查询

使用GROUP BYORDER BY

SELECT store_name,
COUNT(*) AS sale_item
FROM sale 
GROUP BY store_name
ORDER BY COUNT(*) DESC;

Fiddle demo

答案 2 :(得分:1)

您可以使用COUNTGROUP BYORDER BY

SELECT store_name, COUNT(*) AS store_sale FROM sale GROUP BY store_name ORDER BY store_sale DESC;

答案 3 :(得分:0)

这是SQL请求:

SELECT store_name, count(*) AS nbr_sale_item 
FROM sale GROUP BY store_name 
ORDER BY nbr_sale_item DESC

此请求应显示您期望的内容

答案 4 :(得分:0)

SELECT store_name,COUNT(*) AS sale_item 
FROM sale 
GROUP BY store_name 
ORDER BY COUNT(*) DESC;

答案 5 :(得分:-1)

SELECT store_name, COUNT(*) AS sale_item FROM sale GROUP BY store_name ORDER BY COUNT(*) DESC;