Query to combine two tables group by month - mysql

I have tried to connect two tables by join and group them to get the count. But unfortunately, these two tables don't have any common value to join (Or I have misunderstood the solutions).
select date_format(check_in.date,'%M') as Month, count(check_in.Id) as checkInCount
from check_in
group by month(check_in.date);
Month
checkInCount
July
1
October
2
This is the first table.
select date_format(reservation.date,'%M') as Month, count(reservation.id) as reserveCount
from reservation
group by month(reservation.date);
Month
reserveCount
July
3
October
5
This is the second table.
I want to show these two tables in one table.
Month
checkInCount
reserveCount
July
1
3
October
2
5
Thank you for trying this and sorry if this is too easy.

You will need to join the result by month from your two subqueries.
This query assume all the month (July, August, September...) present in your subqueries monthCheckInStat, monthCheckOutStat, even if the count is 0
SELECT monthCheckInStat.Month, monthCheckInStat.checkInCount, monthCheckOutStat.reserveCount
FROM
(
select date_format(check_in.date,'%M') as Month, count(check_in.Id) as checkInCount
from check_in
group by month(check_in.date)
) monthCheckInStat
INNER JOIN
(
select date_format(reservation.date,'%M') as Month, count(reservation.id) as reserveCount
from reservation
group by month(reservation.date)
) monthCheckOutStat
ON monthCheckInStat.Month = monthCheckOutStat.Month;

Related

Joining table based on Month in SQL Query

here i have 2 table that i wanted to join.
i have two table. which is table1 and table2. i only able to union between these two tables. Below is my current output table.
However, my expected output is should be like this:
year
month
usage
2022
7
432.738
2022
8
552.306
2022
9
3148.40500
i wanted to join table1 and table2 by sum of usage column based on the month.
Appreciate your help.
You can do SUM/GROUP BY from your query.
select year, month, sum(usage) as usage
from (
YOUR QUERY IN THE PICTURE
) as q
group by year, month
For the future, paste the query as text, not as an image

MYSQL Finding how many users transacted per month

I have been tasked to find how many users performed a transaction in every month in 2020
I know i have two tables to work with.
Table Name: Receipts|Columns: receipt_id, collection_id, user_id, amount
Table Name: Games |Columns: game_id, collection_id, game_date_time
i tried this but I dont think it makes sense or works
select month(games.game_date_time) AS Month, sum(receipts.id) from bills
join games on bills.collection_id = games.collection_id
WHERE YEAR(games.game_date_time) = 2020
group by receipts.user_id, month(games.game_date_time)
order by month(games.game_date_time)
Use COUNT() to get a count, not SUM(). And if you want a count of users, without counting the same user twice, use COUNT(DISTINCT user_id), don't put user_id in the grouping.
SELECT MONTH(g.game_date_time) AS month, COUNT(DISTINCT r.user_id) AS users
FROM receipts AS r
JOIN games AS g ON r.collection_id = g.collection_id
WHERE YEAR(g.game_date_time) = 2020
GROUP BY month
ORDER BY month
find how many users performed a transaction in every month in 2020
SELECT COUNT(r.user_id)
FROM receipts AS r
JOIN games AS g USING (collection_id)
WHERE YEAR(g.game_date_time) = 2020
GROUP BY r.user_id
HAVING COUNT(DISTINCT MONTH(g.game_date_time)) = MONTH(CURRENT_DATE)
This query:
Selects rows for current year only.
For each user - calculates the amount of distinct months for payments for this user and compares with current month. If user has payments in each month (including current!) these values are equal.
Count the amount of users matched this condition.
PS. The query will fail in 2021 - for to receive correct info in future use
HAVING COUNT(DISTINCT MONTH(g.game_date_time)) = CASE YEAR(CURRENT_DATE)
WHEN 2020
THEN MONTH(CURRENT_DATE)
ELSE 12
END

How get earnings per month MYSQL Query

I have two tables, one table called "tbl_materiales", and another called "tbl_pedidos".. In the table called tbl_materiales" I have information about all my products, Like Description, and the most important "Price"...
In my table "tbl_pedidos", i register all information of products that the user register in the website.
For example:
tbl_materiales:
IdProduct Description Price
5 Product one 8
6 Product three 10
7 Product four 15
tbl_pedidos
IdProduct Quantity Month
5 10 January
6 5 January
7 2 February
So, I want to know all the earnings PER month...
I want to have this: The column earnings is the multiplication of tbl_pedidos.Quantity * tbl_materiales.Price, obviously it depends of the price of the product, and the quantity sold out.
Month Earnings
January 130
February 30
Now, I have this, but it doesn't bring me the correct information...
SELECT tbl_pedidos.Mes
, (SUM(tbl_pedidos.Cantidad) * tbl_materiales.Precio) as Total
FROM tbl_pedidos
INNER JOIN tbl_materiales
ON tbl_pedidos.IdMaterial = tbl_materiales.IdMaterial
GROUP BY tbl_pedidos.Mes
ORDER BY tbl_pedidos.Fecha;
SELECT tbl_pedidos.Mes , SUM(tbl_pedidos.Cantidad*tbl_materiales.Precio) as Total
FROM tbl_pedidos
INNER JOIN tbl_materiales
ON tbl_pedidos.IdMaterial = tbl_materiales.IdMaterial
GROUP BY tbl_pedidos.Mes
ORDER BY tbl_pedidos.Fecha;
Check http://sqlfiddle.com/#!9/de665b/1
The query can be like :
SELECT tbl_p.Month
,sum(as tbl_m.Price*TP.Quantity) AS Earnings
FROM tbl_materiales AS tbl_m
JOIN tbl_pedidos AS tbl_p
ON tbl_m.IdProduct = tbl_p.IdProduct
GROUP
BY tbl_p.Month;
In this case I have used Where instead of Join, maybe de next sentence resolve your problem:
select TP.Month,sum(TM.Price*TP.Quantity) as Earnings
from TBL_Pedidos TBP,TBL_Materiales TM
where TP.IdProduct = TM.Id_Product
group by TP.Month
Group by is the solution

Get total of rows in a mysql table grouped by months and years

I'd like to create some statistics and I want to them in an array grouped by months and years.
Got an MySQL table_users with column user_id.
I want build up a mysql query to list how many members we had in total at the end of each month. The result of the query should be:
Year: Month: Members:
--------------------------
2014 12 11345
2015 1 17939
2015 2 25003
2015 3 32667
There is also the column user_signupdate with the UNIX timestamp when the user_id was added. Whatever I've tried so far, I'm getting only the growing of user_id's, but I'd like to get the total of all user_id's we had for each month and year.
Is it possible to count and group this with only one MySQL query?
The following code will perform simple arithmetic calculation to generate the members running total. See SQL Fiddle demo.
select
t1.year,
t1.month,
(#rtotal := #rtotal + t1.members) AS members
from
(select year(user_signupdate) as year, month(user_signupdate) as month, count(user_id) as members
from table_users
group by year(user_signupdate), month(user_signupdate)
order by year(user_signupdate), month(user_signupdate)) as t1, (Select #rtotal:=0) as rt

sql multiple columns plus sum of each column

Using MySQL, I am counting the occurrence of several events (fields) over a time span of years. I then display this in columns by year. My query works perfect when grouped by year. I now want to add a final column which displays the aggregate of the years. How do I include the total of columns query?
Event 2008 2009 2010 2011 total
A 0 2 0 1 3
B 1 2 3 0 6
etc.
Here is the real query:
select
count(*) as total_docs,
YEAR(field_document_date_value) as doc_year,
field_document_facility_id_value as facility,
IF(count(IF(field_document_type_value ='LIC809',1, NULL)) >0,count(IF(field_document_type_value ='LIC809',1, NULL)),'-') as doc_type_LIC809,
IF(count(IF(field_document_type_value ='LIC9099',1, NULL)) >0,count(IF(field_document_type_value ='LIC9099',1, NULL)),'-') as doc_type_LIC9099,
IF(count(field_document_f1_value) >0,count(field_document_f1_value),'-') as substantial_compliance,
IF(count(field_document_f2_value) >0,count(field_document_f2_value),'-') as deficiencies_sited,
IF(count(field_document_f3_value) >0,count(field_document_f3_value),'-') as admin_outcome_809,
IF(count(field_document_f4_value) >0,count(field_document_f4_value),'-') as unfounded,
IF(count(field_document_f5_value) >0,count(field_document_f5_value),'-') as substantiated,
IF(count(field_document_f6_value) >0,count(field_document_f6_value),'-') as inconclusive,
IF(count(field_document_f7_value) >0,count(field_document_f7_value),'-') as further_investigation,
IF(count(field_document_f8_value) >0,count(field_document_f8_value),'-') as admin_outcome_9099,
IF(count(field_document_type_a_value) >0,count(field_document_type_a_value),'-') as penalty_type_a,
IF(count(field_document_type_b_value) >0,count(field_document_type_b_value),'-') as penalty_type_b,
IF(sum(field_document_civil_penalties_value) >0,CONCAT('$',sum(field_document_civil_penalties_value)),'-') as total_penalties,
IF(count(field_document_noncompliance_value) >0,count(field_document_noncompliance_value),'-') as total_noncompliance
from rcfe_content_type_facility_document
where YEAR(field_document_date_value) BETWEEN year(NOW()) -9 AND year(NOW())
and field_document_facility_id_value = :facility
group by doc_year
You can not GROUP row twice in a SELECT, so you can only count row in a year or in total. You can UNION two SELECT (one grouped by year, second not grouped - total) to overcome this limitation, but I think it is better to count total from year result in script if there is any.
Simplified example:
SELECT by_year.amount, years.date_year FROM
-- generating years pseudo table
(
SELECT 2008 AS date_year
UNION ALL SELECT 2009
UNION ALL SELECT 2010
UNION ALL SELECT 2011
) AS years
-- joining with yearly stat data
LEFT JOIN
(
SELECT SUM(value_field) AS amount, YEAR(date_field) AS date_year FROM data
GROUP BY YEAR(date_field)
) AS by_year USING(date_year)
-- appending total
UNION ALL SELECT SUM(value_field) AS amount, 'total' AS date_year FROM data
WITH ROLLUP is your friend:
http://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html
Use your original query and simply add this to the last line:
GROUP BY doc_year WITH ROLLUP
That will add a final cumulative row to your query's result set.