How to perform OR condition in django queryset?
- Is it possible to specify a condition in Count()? I would like to count only the rows that have, for example, "Manager" in the Position column.
I want to do it in the count statement, not using WHERE; I'm asking about it because I need to count both Managers and Other in the same SELECT (something like Count(Position = Manager), Count(Position = Other)) so WHERE is no use for me in this example.
To solve sql count with condition, you can't limit the query itself with a WHERE clause. But you can use the count aggregate as it only counts the non-null values.
So, use this code:
SELECTcount (case Position when 'Manager' then 1 else null end)
FROM Table_Name
You can also use the sum aggregate in a similar way:
Select Sum (case Job_role when 'HR Associate' then 1 else 0 end)
fromEmployee_Table