user2238244 user2238244 - 4 years ago 203
SQL Question

SUM ADD Different Table same ID

I have no idea of how to resolve this..

SELECT c.NIF,
v.preu
FROM CLIENTS c
INNER JOIN RESERVES_VIATGES r
INNER JOIN VIATGES v
ON r.CODI_VIATGE = v.CODI
ON c.NIF = r.NIF_CLIENT
GROUP BY c.NIF, v.preu;


This query returns 2 column like:

NIF PRICE
111 200
222 600
111 100


I want to select the SUM of price that have the same NIF.
How Can I do it? Thanks

Answer Source

Use the SUM() aggregate function and remove v.preu from your GROUP BY clause.

SELECT c.NIF,
  SUM(v.preu) preu
FROM CLIENTS c
INNER JOIN RESERVES_VIATGES r
ON c.NIF         = r.NIF_CLIENT
INNER JOIN VIATGES v
ON r.CODI_VIATGE = v.CODI
GROUP BY c.NIF;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download