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.
Related
I need to get accumulated number of users by a range of dates i.e. for a month by date. The following query works fine but I have to run it for each date and I cannot use group by date. Please advise.
MySQL version 8
Sample Data
+------------------------+
| id | Registration_Date |
+------------------------+
| 1 | 2020-05-01 |
| 2 | 2020-05-01 |
| 3 | 2020-05-02 |
| 4 | 2020-05-03 |
| 5 | 2020-05-04 |
+------------------------+
Current Query
SELECT COUNT(id) AS 'Registrations'
FROM users
WHERE DATE(Registration_Date) <= "2020-05-04";
Desired Result
+-----------------------------------+
| Registration_Date | Registrations |
+-----------------------------------+
| 2020-05-01 | 2 |
| 2020-05-02 | 3 |
| 2020-05-03 | 4 |
| 2020-05-04 | 5 |
+-----------------------------------+
You can use window functions to achieve the result you want, COUNTing id values on or before the current registration date. Note we use DISTINCT to avoid duplication of entries where multiple users register on the same day:
SELECT DISTINCT Registration_Date,
COUNT(id) OVER (ORDER BY Registration_Date) AS Registrations
FROM users
Output:
Registration_Date Registrations
2020-05-01 2
2020-05-02 3
2020-05-03 4
2020-05-04 5
Demo on dbfiddle
To deal with the case where there are registrations before the first reporting date of interest, you need to count registrations up to and including the first date and then for each date in the reporting period in a derived table, and then sum those in an outer query:
SELECT Reporting_Date,
SUM(Registrations) OVER (ORDER BY Reporting_Date) AS Registrations
FROM (
SELECT '2020-05-01' AS Reporting_Date, COUNT(id) AS Registrations
FROM users
WHERE Registration_Date <= '2020-05-01'
UNION
SELECT Registration_Date, COUNT(id)
FROM users
WHERE Registration_Date BETWEEN '2020-05-02' AND '2020-05-04'
GROUP BY Registration_Date
) r
Generating the result this way in general will be more efficient than wrapping the original query as a derived table as it will require fewer aggregations.
Demo on dbfiddle
I used Nick's answer as source and now modified it a bit to get grand total plus daily increment value.
SELECT Reporting_Date, Registrations FROM
(SELECT DISTINCT DATE(Registration_Date) AS Reporting_Date,
COUNT(id) OVER (ORDER BY DATE(Registration_Date)) AS Registrations
FROM users) AS RAW_Result
WHERE Reporting_Date BETWEEN "2020-05-01" AND "2020-05-04";
Result:
+-----------------------------------+
| Registration_Date | Registrations |
+-----------------------------------+
| 2020-05-01 | 1200 | (grand total until this date)
| 2020-05-02 | 1201 | (grand total + daily increment)
| 2020-05-03 | 1202 |
| 2020-05-04 | 1203 |
+-----------------------------------+
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;
I have a MYSQL database with the following fields (simplified to show issue):
id | courseID | date1 | date2 | date 3 | date 4
-----------------------------------------------------------------
1 | 10 | 2013-01-01 | 2013-02-05 | 0000-00-00 | 0000-00-00
2 | 23 | 2013-03-02 | 2013-05-04 | 2013-07-05 | 0000-00-00
3 | 35 | 2013-02-01 | 0000-00-00 | 0000-00-00 | 0000-00-00
My question is what query could I use to find all the courses with a date less than or equal to today in one of the four fields ordered by date descending. So for my database above the query would give me:
id | courseID | date
---------------------------
1 | 10 | 2013-02-05
3 | 35 | 2013-02-01
1 | 10 | 2013-01-01
Many thanks for your help.
If you can't normalize your table, you could use a UNION query, like this:
SELECT id, courseID, date1 date
FROM yourtable
WHERE date1>'0000-00-00' and date1<=curdate()
UNION
SELECT id, courseID, date2 date
FROM yourtable
WHERE date2>'0000-00-00' and date2<=curdate()
UNION
SELECT id, courseID, date3 date
FROM yourtable
WHERE date3>'0000-00-00' and date3<=curdate()
UNION
SELECT id, courseID, date4 date
FROM yourtable
WHERE date4>'0000-00-00' and date4<=curdate()
ORDER BY date
See fiddle here.
Since I am using UNION, all duplicates will be removed. If you don't want to remove duplicates, you could use UNION ALL instead.
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
I'm using two tables in the database. These tables look like this:
Table A:
id | date
----------------------
12001 | 2011-01-01
13567 | 2011-01-04
13567 | 2011-01-04
11546 | 2011-01-07
13567 | 2011-01-07
18000 | 2011-01-08
Table B:
user | date | amount
----------------------------------
15467 | 2011-01-04 | 140
14568 | 2011-01-04 | 120
14563 | 2011-01-05 | 140
12341 | 2011-01-07 | 140
18000 | 2011-01-08 | 120
I need a query that will join these the two tables.
The first query should result in a total number of users from table A group by date and the number of unique users from table A grouped by date. That query looks like:
SELECT COUNT(DISTINCT id) AS uniq, COUNT(*) AS total, format_date(date, '%Y-%m-%d') as date FROM A GROUP BY date
From the second table I need the sum of the amounts grouped by dates.
That query looks like:
SELECT SUM(amount) AS total_amount FROM B GROUP BY DATE_FORMAT( date, '%Y-%m-%d' )
What I want to do is to merge these two queries into one on column "date", and that as a result I get the following list:
date | unique | total | amount
-----------------------------------------------
2011-01-01 | 1 | 1 | 0
2011-01-04 | 1 | 2 | 260
2011-01-05 | 0 | 0 | 140
2011-01-07 | 2 | 2 | 140
2011-01-08 | 1 | 1 | 120
How can I do that using one query?
Thanks for all suggestions.
select date_format(a.date, '%Y-%m-%d') as date, a.uniq, a.total, ifnull(b.amount, 0) as amount
from (
select count(distinct id) as uniq, count(*) as total, date
from tablea
group by date
) a
left join (
select sum(amount) as amount, date
from tableb
group by date
) b on a.date = b.date
order by a.date
I assume that field date is a datetime type. It's better to format output fields in final result set (date field in this case).
Your queries are fine everything they need is a join.