noMAD
noMAD

Reputation: 7844

SQL query to group by day but with another condition

I have data something like this:

| visitor_id | time       | my_flag |
| 1          | 01-01-2017 | 1       |
| 2          | 01-01-2017 | 1       |
| 1          | 01-02-2017 | 2       |
| 3          | 01-02-2017 | 2       |

etc...

I want to group all visitors in one day but based on a flag. The flag can have two values, 1 or 2. So I need something like

| 12 | 01-01-2017 | 1 |
| 10 | 01-01-2017 | 2 |
| 34 | 01-02-2017 | 1 |

I have

select v.visitor_id v.time, v.flag
from my_table as v
group by DAY(v.time) having flag=1;

But I am not able to figure out how to add multiple conditions (flag=1 and flag=2)? Any pointers?

Upvotes: 0

Views: 44

Answers (2)

Muthaiah PL
Muthaiah PL

Reputation: 1158

SELECT v.time, 
       v.flag, 
       COUNT(v.visitor_id)
FROM my_table as v
GROUP BY v.time, 
         v.flag;`

Upvotes: 1

asmgx
asmgx

Reputation: 8014

try something like this

select v.visitor_id v.time, v.flag
from my_table as v
where v.flag in (1,2)
group by DAY(v.time), v.flag 

Upvotes: 1

Related Questions