I have the following table
id date time_stamp licenseid storeid deviceid value
1 2015-06-12 17:36:15 lic0001 1 0add 52
2 2015-06-12 17:36:15 lic0002 1 0add 54
3 2015-06-12 17:36:15 lic0003 1 0add 53
4 2015-06-12 17:36:21 lic0001 1 0add 54
5 2015-06-12 17:36:21 lic0002 1 0add 59
6 2015-06-12 17:36:21 lic0003 1 0add 62
7 2015-06-12 17:36:21 lic0004 1 0add 55
8 2015-06-12 17:36:15 lic0001 1 0bdd 53
9 2015-06-12 17:36:15 lic0002 1 0bdd 52
10 2015-06-12 17:36:15 lic0003 1 0bdd 52
I need the count of deviceid based on the number of timestamps it is seen in. So the output would be something like: 0add is seen in 2 timestamps hence the count is 2 whereas 0bdd is seen in one time stamp hence 0bdd has count of 1. The number of licenses corresponding to the device per time stamp is not considered for the count.
date deviceid count
2015-06-12 0add 2
2015-06-12 0bdd 1
I am trying with this query below but unable to verify if it works as the query has been executing for quite some time now and not showing any result :
select date, deviceid, count(deviceid) from my_table group by deviceid, time_stamp
Please note that the number of rows I am running this query on is 2,000,000
EDIT: The column labeled time_stamp
is a TIME
type.
The SQL COUNT function is particularly useful because counts database records based on user-specified criteria. Use it to count all the records in a table, count unique values in a column, or count the number of times records occur that meet certain criteria.
The COUNT function returns the number of rows in a group. The ALL keyword includes duplicate values while the DISTINCT keyword removes the duplicate values in the result. The COUNT (*) returns the number of rows in a query including duplicate rows and rows that contain null values.
Counting Unique Values in a Column Use the COUNT function to identify the number of unique values in a column. In the example, to identify the number of different suppliers whose products appear in the produce department, execute the following query: SELECT COUNT (DISTINCT SupplierID)
The expression is a column of a table where you want to count the value. Another form of the COUNT function that accepts an asterisk (*) as the argument is as follows: The COUNT (*) function returns the number of rows in a table in a query. It counts duplicate rows and rows that contain null values.
I think you need to consider a couple of things here:
The fix to the first one is self explanatory, and for the second one you can change your aggregation to COUNT(DISTINCT timestamp)
. Try this query:
SELECT device_id, date, COUNT(DISTINCT timestamp) AS numRows
FROM myTable
GROUP BY device_id, date;
Here is an SQL Fiddle example using your sample data. It is also worth noting that putting an index on the device_id and date columns may help this query run faster, if this query is still slow for you. See the comments for more discussion on this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With