Reputation: 329
I have the following table:
| id | metrictimestamp | vehicleid |
+----+-----------------+-----------+
| 1 | 20180201025934 | 33089 |
| 2 | 20180201025955 | 34489 |
| 3 | 20180201025959 | 32040 |
I need to group by date(metrictimestamp) and count how many unrepeated "vehicleid" there is for each day, any sugestions?
Upvotes: 0
Views: 33
Reputation: 875
First you need to convert your metrictimestamp field into a date type that mysql understands:
STR_TO_DATE(metrictimestamp, '%Y%m%d%h%i%s')
next you need to extract the date portion of that field and give it an alias (date):
DATE(STR_TO_DATE(metrictimestamp, '%Y%m%d%h%i%s')) date
finally you need to group by the resultant date
and the vehicleid
and filter by repeated records (so only include singletons), so putting it all together:
select DATE(STR_TO_DATE(metrictimestamp, '%Y%m%d%h%i%s')) date, vehicleid from group_test_table group by date, vehicleid having count(vehicleid) = 1;
If I misunderstood your question and you only want the unique vehicleids for any date then:
select distinct DATE(STR_TO_DATE(metrictimestamp, '%Y%m%d%h%i%s')) date, vehicleid from group_test_table group by date, vehicleid;
Upvotes: 1
Reputation: 135
You can use DISTINCT in your query:
SELECT COUNT(DISTINCT metrictimestamp) FROM yourTable
Upvotes: 1