SQL查询计数,然后按升序对计数进行排序

时间:2016-10-22 07:36:09

标签: sql count

我有一个SQL查询来计算至少有15条记录的评分数,然后按平均评分排序。

所以这就是我提出的:

Traceback (most recent call last):
  File "C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Test_10.py", line 31, in test
suffix), 'w')
OSError: [Errno 22] Invalid argument: 'C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Final_result//C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file\\1A4ZHETATM15207  C4B NAD A 501      47.266 101.038   7.214  1.00 11.48           C  \n.txt'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Test_10.py", line 94, in <module>
test()
  File "C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Test_10.py", line 40, in test
out_f.close()
UnboundLocalError: local variable 'out_f' referenced before assignment

它确实过滤了少于15条记录的产品,但结果没有按顺序排序。有什么想法吗?

提前致谢。

修改

名称计数(费率)AVG(费率) product1 20 5 product2 18 5 product3 17 5

2 个答案:

答案 0 :(得分:1)

SELECT * FROM 
(
    SELECT name, COUNT(rate), AVG(rate) as Rate FROM RateTable
    WHERE rating = 5
    GROUP BY name
    HAVING COUNT(rate) > 15
) as Result ORDER BY Rate ASC

答案 1 :(得分:-1)

可能这是由于ORDER BY子句中聚合函数的使用不当,存在一定的局限性。以下链接可以帮助您...... What can an aggregate function do in the ORDER BY clause?

祝你好运。