Joining table based on Month in SQL Query - mysql

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

Related

Query to combine two tables group by month

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;

MS-Access : How to sum multiple values from 2 different tables according to dates with output on 1 date per row

I am still getting started learning Access.
I have 3 tables. Table one has Date as primary key and will have all dates. Tables 2 and 3 (Table 3 is mislabeled in the example image as a second Table 2) will both have 2 columns, Date and Amount. Tables 2 and 3 could have multiple rows with the same date (different amounts) and some may miss dates. I am looking for an output query that would have 1 row for every date in table 2 & 3 that has an amount (some dates may not have an amount in either table) and sums all those amounts for that date in 1 row. Below are example tables and the desired output query. Thanks so much for the newbie help!
I now have this code (Note that I have eliminated Table 1):
SELECT Table2.Dat, Sum(Table2.Amount) AS [Sum Of Amount], Sum(Table2.Tax) AS [Sum Of Tax]
FROM Table2
GROUP BY Table2.Dat;
UNION ALL SELECT Table3.Dat, Sum(Table3.Amount) AS [Sum Of Amount], Sum(Table3.Tax) AS [Sum Of Tax]
FROM Table3
GROUP BY Table3.Dat;
This sums the amounts from same dates for each seperate table, but does not sum the dates for both tables. I imagine it is another GROUP function but I have not been successful in forming it correctly.
Current Results from code above
Try below query.
SELECT tt.mDate AS TransactionDate, Sum(tt.SumOfAmount) AS AmountTotal
FROM (SELECT Table2.tDate as mDate, Sum(Table2.Amount) AS SumOfAmount
FROM Table2
GROUP BY tDate
UNION
SELECT Table3.tDate As mDate, Sum(Table3.Amount) AS SumOfAmount
FROM Table3
GROUP BY tDate) AS tt
GROUP BY tt.mDate;

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

MySQL query for each subgroup

I have a table like this:
Year Month Code
1850 January 5210
1850 February 3524
1851 January 6752
1851 January 9877
1851 February 3698
I want to delete repeated months within a year (e.g. 1851 January). I donĀ“t mind loosing one code (6752 or 9877). I thought of using:
Select * from table1 group by Month
But I need to group for each year. Otherwise I will select only one January from the three in the table, and I need to select two of them (one in 1850 and one in 1851).
Of course my table is huge and I cannot do it manually. Thanks.
If you want to have only the entries with count>1 then you can do this:
Select year, month, code, count(1) as cnt from table1 group by year, month having cnt>1;
If the table is huge, make sure that both year and month are indexes, otherwise you'll spend lot of time waiting for results.
http://sqlfiddle.com/#!2/eb325/3
UPDATE: for the case where there are more than 2 rows (and actually in general, if you don't care about the lost "code" entries), it might make sense to select one entry from each year-month into a new table (which will leave you with unique year-month combinations) and then discard the old table, like that:
CREATE TABLE table1_temp SELECT year, month, MIN(code) as code FROM table1 GROUP BY year, month;
DROP TABLE table1;
RENAME TABLE table1_temp TO table1;
http://sqlfiddle.com/#!2/113954/1
Query suggested by #Ashalynd will work if you have only 2 duplicate rows but it will not work if you have 3 rows for year 1951 and month January ...Below query will take care it. You can remove all rows getting from below query.
SELECT
DISTINCT b.CODE
FROM
(SELECT YEAR, MONTH, CODE, COUNT(1) AS cnt FROM table1 GROUP BY YEAR, MONTH HAVING cnt>1) a,
table1 b
WHERE a.year=b.year AND a.month=b.month AND a.code<>b.code;
Another approach can be AS per below-
CREATE TABLE table1_new LIKE table1;ALTER TABLE table1_new ADD UNIQUE KEY (YEAR,MONTH);INSERT IGNORE INTO table1_new SELECT * FROM table1;
TRUNCATE TABLE table1;INSERT INTO table1 SELECT * FROM table1_new;
DROP TABLE table1_new;
Note: IF you want TO keep your VALUES UNIQUE based ON these FIELDS THEN CREATE UNIQUE INDEX ON your main table.
try this:
Select *, count(month) as cnt from table1 group by Year, Month;
You will get all the months under the different years and either one of the repeating months eliminated.
this

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.