Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL:Count of values in one column in relation to another column

Tags:

sql

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

  1. Is the above query right for my output
  2. If so how can I optimize it to run fast for my table size

EDIT: The column labeled time_stamp is a TIME type.

like image 969
fheo Avatar asked Jul 01 '15 15:07

fheo


People also ask

What is the use of count function in SQL?

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.

What is the difference between count() and all() in SQL?

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.

How do you count unique values in a column in SQL?

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)

How do you count the number of rows in a column?

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.


1 Answers

I think you need to consider a couple of things here:

  • If you want the number of timestamps per device for each date, you should be grouping by device and date, not device and timestamp.
  • You have rows where a device id has the same date and timestamp, so you may want to consider looking for distinct timestamps in each date.

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.

like image 80
AdamMc331 Avatar answered Oct 06 '22 00:10

AdamMc331