我正在尝试从我的数据库中找出最畅销的商品,我有3个表,se_order
,se_items
,se_ordered_item
。我尝试了下面的SQL,但是我一直陷入问题,我没有看到错误在哪里:
select i.name, count(max(oi.item_id*oi.quantity))
from SE_ORDERED_ITEM oi, se_items i, se_order o
where o.order_id = oi.order_id
and oi.item_id = i.item_id
group by i.name, i.item_id"
我想列出最畅销商品的名称以及这些商品的数量
但是我收到了这个错误:
ORA-00937: not a single-group group function;
所以任何人都可以在这个问题上帮助我
提前致谢
PS,我正在使用apex oracle环境
答案 0 :(得分:2)
虽然Oracle允许嵌套聚合函数,但我认为你想要这样的东西:
select i.name, sum(oi.quantity)
from SE_ORDERED_ITEM oi JOIN
se_items i
on oi.item_id = i.item_id JOIN
se_order o
on o.order_id = oi.order_id
group by i.name
order by sum(oi.quantity) desc;
注意:
select
使用sum()
的更改。join
。from
oi.item_id
删除group by
。order by
条款。答案 1 :(得分:0)
您可能想尝试rownum
select i.name, sum(oi.quantity)
from SE_ORDERED_ITEM oi, se_items i, se_order o
where o.order_id = oi.order_id
and oi.item_id = i.item_id
and rownum <= 10
group by i.name
order by sum(oi.quantity) desc;
或者,如果您使用的是APEX,只需使用Dinamyc PL / SQL区域:
DECLARE
CURSOR items is
select i.name, sum(oi.quantity)
from SE_ORDERED_ITEM oi, se_items i, se_order o
where o.order_id = oi.order_id
and oi.item_id = i.item_id
group by i.name
order by sum(oi.quantity) desc;
counter NUMBER;
BEGIN
counter := 0;
for i in items loop
counter := counter + 1;
htp.p(i.name);
exit when counter = 10;
end loop;
END;