I have two tables
table1 table2
numID|counts|date numID|net|tax|date
1 1 xy 1 2 1 xy
2 1 xy 2 2 1 xy
3 1 xy 3 2 1 xy
3 1 xy 4 1 1 xy
4 0 xy
So I’d need to sum (net + tax) on table2 for rows that have same date,numID and counts=1.
I tried to do where clause but numID 3 would be added two times but i need it to sum only once. numID 4 should be omitted because it has counts=0.
My select:
SELECT
SUM(`net`+`tax`)
FROM table1 AS d, `table2` AS c
WHERE d.date=c.date
AND d.numID=c.numID
AND `it_counts` = 1
This adds numID 3 two times but it should be DISTINCT.
Try rewriting this statement with a correlated
existsquery: