У меня есть таблица под названием «продукты», где у нее есть 100 записей с информацией о продажах. Мое требование настолько просто, что я не смог этого сделать.pgsql -Показать топ-10 продуктов и других продуктов как «другие» и ее сумма продаж
Мне нужно показать 10 наименований продуктов с названиями продаж и других продуктов как «другие» и их продажи. так что полностью мой o/p будет 11 строк. 11-я строка должна быть другими и суммой продаж всех остальных продуктов. Может ли кто-нибудь дать мне логику?
O/р должна быть такой,
Name sales
------ -----
1 colgate 9000
2 pepsodent 8000
3 closeup 7000
4 brittal 6000
5 ariies 5000
6 babool 4000
7 imami 3000
8 nepolop 2500
9 lactoteeth 2000
10 menwhite 1500
11 Others 6000 (sum of sales of remaining 90 products)
вот мой SQL запрос,
select case when rank<11 then prod_cat else 'Others' END as prod_cat,
total_sales,ID,rank from (select ROW_NUMBER() over (order by (sum(i.grandtotal)) desc) as rank,pc.name as prod_cat,sum(i.grandtotal) as total_sales, pc.m_product_category_id as ID`enter code here`
from adempiere.c_invoice i join adempiere.c_invoiceline il on il.c_invoice_id=i.c_invoice_id join adempiere.m_product p on p.m_product_id=il.m_product_id join adempiere.m_product_category pc on pc.m_product_category_id=p.m_product_category_id
where extract(year from i.dateacct)=extract(year from now())
group by pc.m_product_category_id) innersql
order by total_sales desc
о/р, что я получил есть
prod_cat total_sales id rank
-------- ----------- --- ----
BSHIRT 4511697.63 460000015 1
BT-SHIRT 2725167.03 460000016 2
SHIRT 2630471.56 1000003 3
BJEAN 1793514.07 460000005 4
JEAN 1115402.90 1000004 5
GT-SHIRT 1079596.33 460000062 6
T SHIRT 446238.60 1000006 7
PANT 405189.00 1000005 8
GDRESS 396789.02 460000059 9
BTROUSER 393739.48 460000017 10
Others 164849.41 1000009 11
Others 156677.00 1000008 12
Others 146678.00 1000007 13
Итак, вы можете выяснить, как получить первые 10 строк? – Strawberry
да, я могу выяснить первые 10 строк, заказывая продажи по убыванию –
Я думаю, это может помочь: [link] (http://stackoverflow.com/questions/26420797/tsql-select-top-10-display-the-rest -as-other) – OferP