根据特定值顺序对SQL Oracle查询进行分类
我的任务是首先显示类别5中的产品数量,然后在类别1中显示。这是我拥有的查询:
SELECT
pc.category_name, p.category_id, COUNT(p.category_id) AS num_of_products
FROM
products p
INNER JOIN
product_categories pc
ON
p.category_id = pc.category_id
WHERE
p.category_id != 4 AND pc.category_id != 4
GROUP BY
pc.category_name, p.category_id;
这是输出:
**CATEGORY_NAME CATEGORY_ID NUM_OF_PRODUCTS**
CPU 1 70
Video Card 2 50
Storage 5 108
如何以必需的方式对其进行排序?也许以后代顺序显示num_of_products列?
提前致谢。
My task is to display first the number of products in category 5, then category 1 and then 2. This is the query I have:
SELECT
pc.category_name, p.category_id, COUNT(p.category_id) AS num_of_products
FROM
products p
INNER JOIN
product_categories pc
ON
p.category_id = pc.category_id
WHERE
p.category_id != 4 AND pc.category_id != 4
GROUP BY
pc.category_name, p.category_id;
And this is the output:
**CATEGORY_NAME CATEGORY_ID NUM_OF_PRODUCTS**
CPU 1 70
Video Card 2 50
Storage 5 108
How can I sort it in the required way? Maybe displaying the num_of_products column in a descendent order?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
case
case by 条款中的表达式将不同的类别转化为订单优先级:如果您确实是指在降序顺序(而不是由恰好与之相对应的类别订购),则只需使用
num_of_products
num_of_products >在订单中
子句:Use a
CASE
expression in theORDER BY
clause to translate different categories into the order priorities:If you really mean that you want to order by the
num_of_products
in descending order (rather than ordering by the categories that happen to correspond with that) then just use thenum_of_products
in theORDER BY
clause: