Oracle
 sql >> Database >  >> RDS >> Oracle

SQL SELECT Somma i valori senza includere i duplicati

Dovresti utilizzare le sottoquery su int ed ext per eseguire la somma e unirti alle sottoquery.

Suggerisco anche di utilizzare JOIN espliciti anziché table, table, table

es.

SELECT emp.Name, int.Amount AS InternalCostTotal, ext.Amount AS ExternalCostTotal
FROM Employees emp
JOIN ( 
    SELECT EmployeeID, SUM(Amount) AS Amount 
    FROM InternalCosts 
    GROUP BY EmployeeID 
) int ON emp.EmployeeId = int.EmployeeID
JOIN ( 
    SELECT EmployeeID, SUM(Amount) AS Amount 
    FROM ExternalCosts 
    GROUP BY EmployeeID 
) ext ON emp.EmployeeId = ext.EmployeeID