Reputation: 488
I am using MySQL.
Let's take an example.
table_a
number value
1 1
1 1
1 0
2 1
2 1
3 1
3 0
I want all distinct rows which do not have any value 0 in it, as in the above case expected result is 2. As we know MySQL doesn't support minus operator, can somebody help me out with the query.
Edit: Answer should be 2 by mistake i changed it to 1(edited).
Upvotes: 1
Views: 485
Reputation: 3756
Try the following:
SELECT
number,
DISTINCT(value)
FROM table_a
WHERE value > 0;
Upvotes: 0
Reputation: 72185
Try this:
SELECT number
FROM mytable
GROUP BY number
HAVING COUNT(CASE WHEN value = 0 THEN 1 END) = 0
HAVING
clause filters out number
slices containing at least one 0
value.
Or, alternatively:
SELECT number
FROM mytable
GROUP BY number
HAVING MIN(value) > 0
The above query selects number
groups containing only positive, non-zero, values.
Upvotes: 0
Reputation: 522211
Here is an alternative to using the DISTINCT
keyword:
SELECT number, value
FROM table_a
GROUP BY number, value
HAVING SUM(value) > 0
If you want the number of records where both the number
and value
equal 1, then I would try the following:
SELECT COUNT(*)
FROM table_a
WHERE number = 1 AND value = 1
Upvotes: 1
Reputation: 4844
Both column Distinct like this
SELECT DISTINCT number, value FROM table_a
WHERE value <> 0;
Upvotes: 0