I have problems when I want to generate a query in posgretsql, I was trying and I do not know how to calculate the following
My table has the following data:
CREATE TABLE "abc-ventas"
AS
SELECT *
FROM ( VALUES
( '0001', 'sandía' , 2.000, 3.000 ),
( '0002', 'lápiz' , 3.000, 5.000 ),
( '0003', 'manzana', 1.000, 2.000 ),
( '0004', 'naranja', 2.000, 3.000 ),
( '0005', 'arroz' , 1.000, 5.000 )
) AS t(CODIGO, DESCRIPCION, COSTO, NETO);
I need to calculate the following information
PARTIC= NETO / SUM(TOTAL NETO)
CODIGO DESCRIPCION COSTO NETO PARTIC PARTIC-ACUM
0001 sandía 2.000 3.000 17%
0002 lápiz 3.000 5.000 28%
0003 manzana 1.000 2.000 11%
0004 naranja 2.000 3.000 17%
0005 arroz 1.000 5.000 28%
-------------
18.000 100%
Now to calculate the "partic-acum" order DESC "partic"
PARTIC-ACUM= PARTIC + PREVIOUS PARTIC
CODIGO DESCRIPCION COSTO NETO PARTIC PARTIC-ACUM CLASIF
0002 lápiz 3.000 5.000 28% 28%
0005 arroz 1.000 5.000 28% 56%
0001 sandía 2.000 3.000 17% 72%
0004 naranja 2.000 3.000 17% 89%
0003 manzana 1.000 2.000 11% 100%
Once generated the result I want to classify it in A B C
- If the value is 0 to 80: A
- If the value is 81 to 95: B
- If the value is 95 to 100: C
This is my desired output
CODIGO DESCRIPCION COSTO NETO PARTIC PARTIC-ACUM CLASIF
0002 lápiz 3.000 5.000 28% 28% A
0005 arroz 1.000 5.000 28% 56% A
0001 sandía 2.000 3.000 17% 72% A
0004 naranja 2.000 3.000 17% 89% B
0003 manzana 1.000 2.000 11% 100% C
The query I have is the following
SELECT
codigo,
descripcion,
cantidad,
costo,
neto,
(SELECT sum(neto) FROM "abc-ventas") as total,
(round(neto /(SELECT sum(neto) FROM "abc-ventas"),4)*100) AS participacion
FROM "abc-ventas"
ORDER BY participacion DESC;