Count columns according to dates in SQL - mysql

I need help with a SQL statement. The goal is to count the amount of alarms of each date. My table looks something like this:
|----DATE----|---COUNTER---|---ALARM_ID---|
|2012-01-01 | 30 | 1 |
|2012-01-01 | 20 | 2 |
|2012-01-01 | 10 | 3 |
|2012-01-02 | 5 | 1 |
|2012-01-02 | 25 | 2 |
|2012-01-02 | 12 | 3 |
|2012-01-03 | 33 | 1 |
|2012-01-03 | 43 | 2 |
|2012-01-03 | 11 | 3 |
And I'm looking for a SQL statement that gives this result:
|----DATE----|---COUNTER---|
|2012-01-01 | 60 |
|2012-01-02 | 42 |
|2012-01-03 | 87 |
I've been working on this SELECT date, SUM(counter) FROM all_stats but all I get is:
|----DATE----|---COUNTER---|
|2012-01-01 | 60 |
Do I have to create a loop to go through all dates and count?
Thanks in advance, Steve-O

SELECT date, SUM(counter)
FROM all_stats
GROUP BY date

Try this instead
SELECT date, SUM(counter) FROM all_stats GROUP BY date;
"GROUP BY date" puts all the individual dates on a separate line and does the sum separately per date.

select date, sum(counter)
from all_stats
group by date

Related

mysql need complete count of a column and group by some columns

I need a complete count of each person_id from the database according to the date wise report
SELECT date, person_id, count(person_id)
FROM visits
group by date, person_id
I tried this one but this couldn't give the result what I expected.
Date | person_id| count(person_id)
2018-01-01 | 33000 | 10 |
2018-01-01 | 712000 | 111 |
2018-01-01 | 730000 | 30 |
2018-01-01 | 743000 | 5 |
2018-01-01 | 755000 | 123 |
you need total append to your query result? For example:
Date | person_id| count(person_id) | total
2018-01-01 | 33000 | 10 | 1000
2018-01-01 | 712000 | 111 | 1000
right? if so, I don't think it's a good idea only using sql query. On my case, I will query twice asynchronously,and then merge the result.
like this:
query1:
SELECT date, person_id, count(person_id)
FROM visits
group by date, person_id
query2:
SELECT count(person_id) as total
FROM visits
and then merge the results by program.

Count Monthly Visitor based on their Purpose

Hi everybody,
I have this visitors table:
ID | Date | Purpose
1 | 20/10/2016 | Purpose1
2 | 22/10/2016 | Purpose1
3 | 25/10/2016 | Purpose2
4 | 12/11/2016 | Purpose1
5 | 14/11/2016 | Purpose2
6 | 16/11/2016 | Purpose2
Currently I'm using this query:
select case
when date like '%/10/2016' then '10/2016'
when date like '%/11/2016' then '11/2016'
end as month, count(*) as total
from visitors
where
date like '%/10/2016'
or date like '%/11/2016'
GROUP by month
I can only get month and total column count from query above. How can I achieve this output?
Month | Total | Purpose1 | Purpose2
10/2016 | 3 | 2 | 1
11/2016 | 3 | 1 | 2
Thanks!
Consider the following...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,Date DATE NOT NULL
,Purpose VARCHAR(12) NOT NULL
);
INSERT INTO my_table VALUES
(1,'2016-10-20','Purpose1'),
(2,'2016-10-22','Purpose1'),
(3,'2016-10-25','Purpose2'),
(4,'2016-11-12','Purpose1'),
(5,'2016-11-14','Purpose2'),
(6,'2016-11-16','Purpose2');
SELECT DATE_FORMAT(date,'%Y-%m') month
, SUM(purpose = 'purpose1') purpose1
, SUM(purpose = 'purpose2') purpose2
, COUNT(*) total
FROM my_table
GROUP
BY month;
+---------+----------+----------+-------+
| month | purpose1 | purpose2 | total |
+---------+----------+----------+-------+
| 2016-10 | 2 | 1 | 3 |
| 2016-11 | 1 | 2 | 3 |
+---------+----------+----------+-------+
..or (and in my view, better, provided you have access to application code)...
SELECT DATE_FORMAT(date,'%Y-%m') month
, purpose
, COUNT(*) total
FROM my_table
GROUP
BY month
, purpose;
+---------+----------+-------+
| month | purpose | total |
+---------+----------+-------+
| 2016-10 | Purpose1 | 2 |
| 2016-10 | Purpose2 | 1 |
| 2016-11 | Purpose1 | 1 |
| 2016-11 | Purpose2 | 2 |
+---------+----------+-------+
Transposing tables isn't very fast. It is better to do so in some small program.
If you do a
select case
when date like '%/10/2016' then '10/2016'
when date like '%/11/2016' then '11/2016'
end as month, count(*) as total, Purpose
from visitors
where
date like '%/10/2016'
or date like '%/11/2016'
GROUP by month, Purpose
You'll have a good starting point.
You might need to add an ORDER BY clause (depending on your DBMS).
If (and only if) you only have two purposes in your table and the table isn't huge in size, you can create two views and join them.

Count data for 15 minute intervals that are grouped by date

I have the following MySQL table:
+---------------------+-----------+
| timestamp | sensor_id |
+---------------------+-----------+
| 2010-04-09 01:42:31 | M049 |
| 2010-04-09 01:43:31 | M049 |
| 2010-04-09 01:44:31 | M049 |
| 2010-04-09 01:59:31 | M049 |
| 2010-04-10 01:10:31 | M049 |
| 2010-04-10 01:40:31 | M049 |
| 2010-04-10 01:42:31 | M049 |
| 2010-04-11 16:43:31 | M049 |
+---------------------+-----------+
I know how to query the db to get a count of the entries for a specific daytime intervall and group the result by date.
An example to query the event count between 1 am and 2 pm would look like this:
SELECT
date(timestamp) as date,
count(timestamp) as count
FROM
event_data
WHERE
EXTRACT(HOUR FROM TIME(timestamp)) BETWEEN 1 AND 14
GROUP BY
date
The query returns the following table:
+------------+-------+
| date | count |
+------------+-------+
| 2010-04-09 | 4 |
| 2010-04-10 | 3 |
+------------+-------+
Now I only want to count an event every 15 minutes.
The desired result would be:
+------------+-------+
| date | count |
+------------+-------+
| 2010-04-09 | 2 |
| 2010-04-10 | 2 |
+------------+-------+
How do I alter my query to get these results?
You have a good start to group by date and query for the hours you want. Similarly, you can write a query that gets the intervals. I would start by writing a case statement that reads each row you want, and adds a column specifying which interval of the hour it is. (0:14 - 1, 15:29 - 2...) like this:
SELECT timeCol,
HOUR(timeCol) AS hour,
CASE WHEN MINUTE(timeCol) BETWEEN 0 AND 14 THEN 1
WHEN MINUTE(timeCol) BETWEEN 15 AND 29 THEN 2
WHEN MINUTE(timeCol) BETWEEN 30 AND 44 THEN 3
ELSE 4 END AS minute
FROM myTable;
This gives you something like this:
| timeCol | hour | minute |
+---------------------+------+--------+
| 2010-04-09 01:42:31 | 1 | 3 |
| 2010-04-09 01:43:31 | 1 | 3 |
| 2010-04-09 01:44:31 | 1 | 3 |
Once you have that, you can select the distinct hour/minute pairs in each day, and that will give you what you want, as long as you use your WHERE clause accordingly:
SELECT DATE(timeCol) AS dateCol, COUNT(DISTINCT hour, minute) AS numEvents
FROM(
SELECT timeCol,
HOUR(timeCol) AS hour,
CASE WHEN MINUTE(timeCol) BETWEEN 0 AND 14 THEN 1
WHEN MINUTE(timeCol) BETWEEN 15 AND 29 THEN 2
WHEN MINUTE(timeCol) BETWEEN 30 AND 44 THEN 3
ELSE 4 END AS minute
FROM myTable) tmp
WHERE HOUR(timecol) BETWEEN 1 AND 14
GROUP BY dateCol;
Here is an SQL Fiddle example.
I would just like to add that you don't have to record the intervals as 1, 2, 3, 4. Make sure you use something readable, that will make sense to you again in the future. For example, maybe something like this would be better:
WHEN MINUTE(timeCol) BETWEEN 0 and 14 THEN 'firstInterval'...

mysql query get range between date not from table

I have payment table info like this
ID Costumer | start_pay | Payment
1 | 2014-01-01 | 1.500
2 | 2013-12-01 | 900
that information they must pay every month, i want calculating it for range between start_pay to CURDATE
if CURDATE is 2014-03-01 (Y-m-d) the result I want like this
ID Costumer | start_pay | Payment | total_to_pay | month_count
1 | 2014-01-01 | 1.500 | 4.500 | 3
2 | 2013-12-01 | 900 | 3.600 | 4
can i do that with mysql query?
Try
SELECT *,TIMESTAMPDIFF(MONTH, DATE_SUB(start_pay,INTERVAL 1 MONTH), CURDATE()) AS
month_count,Payment * month_count AS total_to_pay FROM TABLE
Note that if the difference is less than a month it will output 0
PERIOD_DIFF is basically made for this type of calculation:
SELECT PERIOD_DIFF(DATE_FORMAT(CURDATE(),'%Y%m'), DATE_FORMAT(start_pay, '%Y%m')) from your table;

MySQL - how to select id where min/max dates difference is more than 3 years

I have a table like this:
| id | date | user_id |
----------------------------------------------------
| 1 | 2008-01-01 | 10 |
| 2 | 2009-03-20 | 15 |
| 3 | 2008-06-11 | 10 |
| 4 | 2009-01-21 | 15 |
| 5 | 2010-01-01 | 10 |
| 6 | 2011-06-01 | 10 |
| 7 | 2012-01-01 | 10 |
| 8 | 2008-05-01 | 15 |
I’m looking for a solution how to select user_id where the difference between MIN and MAX dates is more than 3 yrs. For the above data I should get:
| user_id |
-----------------------
| 10 |
Anyone can help?
SELECT user_id
FROM mytable
GROUP BY user_id
HAVING MAX(`date`) > (MIN(`date`) + INTERVAL '3' YEAR);
Tested here: http://sqlize.com/MC0618Yg58
Similar to bernie's approach, I'd keep date formats native. I'd also probably list the MAX first as to avoid an ABS call (secure a positive number is always returned).
SELECT user_id
FROM my_table
WHERE DATEDIFF(MAX(date),MIN(date)) > 365
DATEDIFF just returns delta (in days) between two given date fields.
SELECT user_id
FROM (SELECT user_id, MIN(date) m0, MAX(date) m1
FROM table
GROUP by user_id)
HAVING EXTRACT(YEAR FROM m1) - EXTRACT(YEAR FROM m0) > 3
SELECT A.USER_ID FROM TABLE AS A
JOIN TABLE AS B
ON A.USER_ID = B.USER_ID
WHERE DATEDIFF(A.DATE,B.DATE) > 365