I have this table customerDetail, in which there’s a field c_type, in which “a” represents “active” and “d” represents “not-active”. Now I have to find the count of both of them in same query.
I used these but no result.
SELECT Count(c_type) AS Active, Count(c_type) AS Not_Active
FROM customerDetail
WHERE c_type="a" OR c_type="d"
of course I know it obviously looks dirty, but I have also tried this, but this didn’t worked either-
SELECT
Count(customerDetail.c_type) AS Active,
Count(customerDetail_1.c_type) AS Not_Active
FROM customerDetail INNER JOIN customerDetail AS customerDetail_1
ON customerDetail.Id=customerDetail_1.Id
WHERE (customerDetail.c_type="a") AND (customerDetail_1.c_type="d")
But again it wasn’t helpful either, so can anyone please tell me how am I supposed to know the count of both active and non-active in same query?
That was for MS Access.
Somehow I missed the
tsqltag when first saw this question. In Transact-SQL you can employ a CASE construct, which can be said of as a more powerful equivalent ofIIFin Access:Actually, in T-SQL I would use COUNT instead of SUM, like this:
Here
1in each CASE expression can be replaced by anything as long as it is not NULL (NULLs are not counted). If the ELSE part is omitted, like in the query above,ELSE NULLis implied.