I’ve got the following Tables:
User 1:n UserFilter n:1 Filter
Filter got an ID and a Name, User got his Name as ID (don’t blame me, not my idea!).
The whole “User ‘a’ got Filters 1, 2 and 3”-Idea is being used in an ASP frontend, not important for the question.
UserFilter simply got 2 columns, User and FilterID, so if there’s a column User a – Filter 1, that means User a is using Filter 1, if there is no such column, it means he’s not.
Now I’d like to perform a SELECT query that returns every filter (there are 8 overall) for a specific user with a custom column called active.
Here’s what I got so far:
select
distinct f.Name, f.Type,
case when uf.userid = 'a' then 1 else 0 end as Active
from filter f
left outer join userfilter uf on f.id = uf.filterid
where uf.userid = 'a' or uf.userid is null
This will return 8 columns, some of them with Active = 0 and some with Active = 1, just as I wanted, BUT if UserFilter contains filters of multiple users, e.g.
a - 1
a - 2
b - 3
b - 7
a - 8
and I run this query for User “a” the query will only return Filter 1 as active, 2 as active, 4 5 and 6 as inactive and 8 as active, completely leaving out 3 and 7.
Any ideas?
Thanks!
The condition that userid=’a’ should be a join condition: