2
Question: Create a Query that produces a report with the following items:
- a) Name of client;
- b) Total value of the claim;
- c) Sort by date (decreasing) and higher value (decreasing);
My difficulty is in making the sum of the total value per request, since the id_pedido is repeated in more than one item (e.g.: Id_request = 1, product value =5; Id_product = 1, ordering value 3), with the need to add them up to reach the total value per id_pedido. Everything must happen within SELECT.
SELECT pe.id_pedido, C.NOME_CLIENTE, PE.DATA_PEDIDO, P.VALOR_PRODUTO * IP.QUANTIDADE AS TOTAL_PED
FROM CLIENTES C
INNER JOIN PEDIDOS PE
ON c.id_cliente = pe.id_cliente
INNER JOIN ITEM_PEDIDOS IP
ON pe.id_pedido = ip.id_pedido
INNER JOIN produtos P
ON ip.id_produto = p.id_produto
ORDER BY pe.data_pedido, pe.id_pedido DESC;

"My difficulty is in making the sum of the total value per request, since the id_request is repeated in more than one item" but in the enunciated does not ask for the id, can simply remove it from the
SELECT, or will not be able to group, group only by name and date– Ricardo Pontual
if I understand correctly you want to group the fields where have the same
id_pedidoadding up their values in the case ofid_pedido= 20 theTOTAL_PEDIDOshall be equal to 23,61. Correct?– André Walker
That’s right, that’s the point!!
– Bia.mts
Have you studied the
GROUP BY?– Woss