Postgresql:如何根据项目占总项目的百分比对项目进行分组

Postgresql:如何根据项目占总项目的百分比对项目进行分组,sql,postgresql,Sql,Postgresql,我想根据他们的店铺id在总销售额中所占的百分比,从上到下50%对他们进行分组,并将底部的产品标记为“其他” 看起来像这样的东西: store_id product_name quantity 1 apple 10 1 orange 12 1 grapes 9 1 mango 17

我想根据他们的店铺id在总销售额中所占的百分比,从上到下50%对他们进行分组,并将底部的产品标记为“其他” 看起来像这样的东西:

    store_id   product_name   quantity
       1          apple          10
       1          orange         12
       1          grapes          9
       1          mango          17
       2          chicken        112
       2          beef           90
       2          pork           89
       2          lamb           115

这可能吗?我真的很难用谷歌搜索这就是我来这里的原因。谢谢

假设tablename为gt,则:

     store_id    product_name    percent
        1           mango         35.42%
        1           orange        25.00%
        1           OTHER         20.83%
        1           OTHER         18.75%
        2           lamb          28.33%
        2           chicken       27.59%
        2           OTHER         22.17%
        2           OTHER         21.92%

假设使用以下代码创建数据:

t=# with a as (select *,round( quantity * 100.0 / sum(quantity) over(partition by store_id), 2) r from gt)
,b as (select *, sum(r) over (partition by store_id order by r desc) from a)
select store_id, case when coalesce(lag(sum) over (partition by store_id) < 50 ,true) then product_name else 'OTHER' end product_name, r||'%' percent from b;
 store_id | product_name | percent
----------+--------------+---------
        1 | mango        | 35.42%
        1 | orange       | 25.00%
        1 | OTHER        | 20.83%
        1 | OTHER        | 18.75%
        2 | lamb         | 28.33%
        2 | chicken      | 27.59%
        2 | OTHER        | 22.17%
        2 | OTHER        | 21.92%
(8 rows)
您可以通过此查询获得所需的输出:

create table product(
  store int, 
  name text,
  qtd int
  );


insert into product values(1,' apple',10);
insert into product values(1,' orange',12);
insert into product values(1,' grapes',9);
insert into product values(1,' mango',17);
insert into product values(2,' chicken',112);
insert into product values(2,' beef',90 );
insert into product values(2,' pork',89 );
insert into product values(2,' lamb',115);
我将这样写:

with total_query as (
  SELECT store, sum(qtd) AS totalamount 
    FROM product 
    GROUP BY store 
  ),
count_query as (
  SELECT store, count(qtd) AS totalcnt 
    FROM product 
    GROUP BY store 

  ),
percent_query as (

select p.store, p.name, 
  round(p.qtd::float/(select totalamount from total_query
          where total_query.store = p.store)*100) as percent

  from product p

)

select store, 
case when row_number() over (
  partition by store order by percent desc)/
(select totalcnt from count_query where store = p.store)::float <= 0.5 
then 'Other' else name end, percent 

from percent_query p order by store, percent

| store |    name | percent |
|-------|---------|---------|
|     1 |  grapes |      19 |
|     1 |   apple |      21 |
|     1 |   Other |      25 |
|     1 |   Other |      35 |
|     2 |    beef |      22 |
|     2 |    pork |      22 |
|     2 |   Other |      28 |
|     2 |   Other |      28 |
请注意,这将为彼此生成一个单独的行,如您的示例中所示。实际上,我想把它们结合起来。如果你想这样做,却不知道怎么做,那就再问另一个问题

select t.*,
       quantity / sum(quantity) over (partition by store_id) as ratio,
       (case when sum(quantity) over (partition by store_id order by quantity desc) - quantity <
                  0.5 * sum(quantity over (partition by store_id) as running_ratio
             then product_name
             else 'Other'
        end) as product_name
from t;