I'm new to sql. Basicially, I'm trying to SUM
an amount from one table, over somekind of ID. I got that part, but I also want to know if that person is (in)active and that's from another table. I know I can use my ID, and I've tryed with a UNION, but no luck so far.
Gives the data p.inactive
.
SELECT p.inactive
FROM deb d
RIGHT JOIN pat p ON(d.debitor = p.cpr);
d.amount
works but p.inactive
does not, because of the LEFT JOIN
SELECT SUM(d.amount), p.inactive
FROM deb d
LEFT JOIN pat p ON(d.debitor = p.cpr)
GROUP BY d.debitor;
So I've tryed to combine them with a UNION, but p.inactive
is still null
( SELECT d.debitor, p.inactive FROM deb d
LEFT JOIN pat p ON d.debitor = p.cpr
)
UNION
(
SELECT d.debitor, p.inactive FROM deb d
RIGHT JOIN pat p ON d.debitor = p.cpr
WHERE d.debitor IS NULL
);
I'm totally stucked now? Can anyone help? Is it even posible without seeing my DB. Let me know if I need to provide more data/info.
---------------------- UPDATE
Table deb
debitor | amount
1...|...10
1...|...20
1...|...45
2...|...30
2...|...10
Table pat
cpr...|...inactive
1...|...0
2...|...1
3...|...0
4...|...0
5...|...1
6...|...1
The person with p.cpr and d.debitor = 1, has a sum amount of 75 and is INACTIVE
The person with p.cpr and d.debitor = 2, has a sum amount of 40 and is ACTIVE
Final Update:
My bad, I assummed there was some deb.debitors in the pat.cpr, and there wasn't, hence the no matches. Next time I will test for that first ofcourse.