Reputation: 3658
I wanted to take count of records based on some condition.
Here's what the condition is.
data:
"col1" "col2" "col3 A Y Y A Y Y A N Y A N Y A N Y A Y A Y A N Y B Y N B Y Y B N Y B N N B Y Y B N Y C Y N C Y Y C N Y C Y Y C N Y C Y Y
I need to take the count of Y
from col3
excluding the N
of col2
where the col1
value is A
. My head is cracking when i think of doing it.
Note : N
values of col2 must be excluded only where the col1 value is A. The null values also need to be included along with Y
passing the criteria where the col3 value is Y
.
Please help me.
I need to get the count as 11
if the query is correct.
Code i tried with.
Select 'Yes' as "label", Count(*) FROM Table1 Where "date" between '2014-03-01' And '2014-03-05' And "col3" = 'Y' and ("col1" 'C' and "col2" 'N')
I have the idea to how to do it orally. By i couldn't figure out a way to do it in PostgreSQL
.
Upvotes: 0
Views: 37
Reputation: 6120
Try with something like this:
Select
'Yes' as "label",
Count(*)
FROM Table1
Where "date" between '2014-03-01' And '2014-03-05'
AND col3 = 'Y' AND NOT (col1='A' AND col2 IS NOT NULL AND col2='N')
Upvotes: 2
Reputation: 1366
You could consider the A
and not A
as two different cases try something like this:
Select 'Yes' as "label", Count(*)
FROM Table1
Where "date" between '2014-03-01' And '2014-03-05'
and "col3" = 'Y'
And (("col1" = 'A' And "col2" <> 'N') Or ("col1" <> 'A'))
Upvotes: 0
Reputation: 125204
select
count(col3 = 'Y' or null) - count(col2 = 'N' and col1 = 'A' or null) as "Yes"
from table1
where
"date" between '2014-03-01' and '2014-03-05'
Upvotes: 0