How to sum the values in Number field in table2 when the ID field is unique and field Type = 3 (range 1-5)?
SELECT TOP (100)
TB1.CarID
FROM table1 as TB1
LEFT JOIN table2 as TB2 on TB1.CarID = TB2.CarID
WHERE
GROUP BY
- Table1
- Table2
I have tried several solutions:
,SUM (CASE WHEN TB2.Type = 3 THEN TB2.Number END)
Return result is incorrect x2, possibly due to a large number of table joins. If you have any comment why the values are displayed x2, please give me a hint. I would like to add an additional condition, i.e. sum only when the value in the id column is unique. I believe this can solve the problem.
,SUM (CASE WHEN TB2.Type = 3 AND TB2.ID is UNIQUE THEN TB2.Number END) [incorrect]
I will be grateful for your help!