关于求和后排序的sql问题

发布于 2024-10-13 02:03:48 字数 199 浏览 3 评论 0原文

我有一个产品表,其中包含一次的所有销售数量..所以该表是:

id |产品部门 ID |产品 ID | amount_sold

我需要列出所有product_department_ids 中最畅销的 2 个产品。我有什么想法可以这样做吗?

如果你能在 pl/sql 中做到这一点那就太好了,但 sql 也可以!

谢谢 !

i have a products table that contains all the sales as in quantity made at a time .. so the table is :

id | product_department_id | product_id | quantity_sold

i need to list for all the product_department_ids the best 2 selling products . Any ideas how i can do so ?

if you can do it in pl/sql it would be great but sql is ok also !

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-10-20 02:03:48
drop table quantity;

create table quantity (
  id number primary key,
  product_department_id number,
  product_id number,
  quantity_sold number,
  unique (product_department_id, product_id)
);

insert into quantity values (1, 1, 1, 10);
insert into quantity values (2, 1, 2, 20);
insert into quantity values (3, 1, 3, 30);

insert into quantity values (4, 2, 1, 60);
insert into quantity values (5, 2, 2, 50);
insert into quantity values (6, 2, 3, 40);


select * from (
 select quantity_sold, product_id, product_department_id,
       row_number() over (partition by product_department_id order by quantity_sold desc) r
  from quantity
) where r < 3;

编辑仍然不确定到底问了什么,但如果组合产品/部门可以有多个条目,那么它将是:

drop table quantity;

create table quantity (
  id number primary key,
  product_department_id number,
  product_id number,
  quantity_sold number
);

insert into quantity values ( 1, 1, 1, 15);
insert into quantity values ( 2, 1, 1, 15);
insert into quantity values ( 3, 1, 1, 15);
insert into quantity values ( 4, 1, 2, 20);
insert into quantity values ( 5, 1, 3, 30);

insert into quantity values (10, 2, 1, 60);
insert into quantity values (11, 2, 2, 50);
insert into quantity values (12, 2, 3, 40);
insert into quantity values (13, 2, 3, 30);


select * from (
 select sum(quantity_sold), 
        product_id, product_department_id,
        row_number() over (partition by product_department_id 
                           order by sum(quantity_sold) desc
        ) r
  from  quantity
  group by product_department_id, product_id
) where r < 3
order by product_department_id, product_id;
drop table quantity;

create table quantity (
  id number primary key,
  product_department_id number,
  product_id number,
  quantity_sold number,
  unique (product_department_id, product_id)
);

insert into quantity values (1, 1, 1, 10);
insert into quantity values (2, 1, 2, 20);
insert into quantity values (3, 1, 3, 30);

insert into quantity values (4, 2, 1, 60);
insert into quantity values (5, 2, 2, 50);
insert into quantity values (6, 2, 3, 40);


select * from (
 select quantity_sold, product_id, product_department_id,
       row_number() over (partition by product_department_id order by quantity_sold desc) r
  from quantity
) where r < 3;

Edit Still not sure about what exactly was asked, but if the combination prodcut/department can have multple entries then it would be:

drop table quantity;

create table quantity (
  id number primary key,
  product_department_id number,
  product_id number,
  quantity_sold number
);

insert into quantity values ( 1, 1, 1, 15);
insert into quantity values ( 2, 1, 1, 15);
insert into quantity values ( 3, 1, 1, 15);
insert into quantity values ( 4, 1, 2, 20);
insert into quantity values ( 5, 1, 3, 30);

insert into quantity values (10, 2, 1, 60);
insert into quantity values (11, 2, 2, 50);
insert into quantity values (12, 2, 3, 40);
insert into quantity values (13, 2, 3, 30);


select * from (
 select sum(quantity_sold), 
        product_id, product_department_id,
        row_number() over (partition by product_department_id 
                           order by sum(quantity_sold) desc
        ) r
  from  quantity
  group by product_department_id, product_id
) where r < 3
order by product_department_id, product_id;
青丝拂面 2024-10-20 02:03:48

如果一种产品只能有一个部门,您只需order by即可:

select  product_department_id
from    YourTable
where   rownum < 3
order by
        quantity_sold desc

If a product can have only one department, you can simply order by:

select  product_department_id
from    YourTable
where   rownum < 3
order by
        quantity_sold desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文