0

I am trying to get a count of Ids using some conditions.

This is my initial Query:

SELECT
    COUNT(DISTINCT (id))
FROM
    my_table
WHERE
    app_id = 1
        AND zone_id != 1
        AND presence_time_secs > 20
        AND initial_timestamp BETWEEN '2017-05-02 00:00:00' AND '2017-05-05 00:00:00'
group by 
    day(initial_timestamp)

So my problem is that is not getting the zone_id!=1 part.

There are a few values when I do the same query using zone_id=1, but if in the first query i take the zone_id!=1it will return exactly the same values.

What am I doing wrong?

(I have also tried using NOT IN, same results).

Thanks in advance!

5
  • Maybe the zone_id!=1 count is as same as zone_id=1 count. Commented Jun 22, 2017 at 12:44
  • But... the zone_id!=1 should take away the results from the other zones correct? Commented Jun 22, 2017 at 12:45
  • Try to count without AND zone_id != 1 and see the result. I think you should query all the result without aggregation to confirm if data is correct or not. Commented Jun 22, 2017 at 12:46
  • Try to see values in this way:SELECT ZONE_ID, COUNT(DISTINCT(Id)) AS RC FROM MYTABLE WHERE app_id = 1 /* AND zone_id != 1 */ AND presence_time_secs > 20 AND initial_timestamp BETWEEN '2017-05-02 00:00:00' AND '2017-05-05 00:00:00' group by day(initial_timestamp) , ZONE_ID Commented Jun 22, 2017 at 12:47
  • You're grouping by day so if there are any days that are not zone 1 they will be counted once. I think you're getting 3 each time (though it's hard to guess without any sample data)? Commented Jun 22, 2017 at 13:04

1 Answer 1

1

Modify this zone_id !=1 with zone_id <> 1 or zone_id NOT LIKE '1'

Sign up to request clarification or add additional context in comments.

1 Comment

tried it but returns the same... at the end i made another select inside the NOT IN and returns different data...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.