Adding values that occur within same month: mysql - mysql

I have a table like this:
Date Name Qty
2016-09-13 00:00:00 John 2
2016-09-15 00:00:00 Matt 3
2016-09-21 00:00:00 Rich 1
2016-09-23 00:00:00 Matt 1
2016-10-05 00:00:00 John 1
2016-10-07 00:00:00 Matt 3
2016-10-12 00:00:00 Rich 0
2016-10-23 00:00:00 Matt 2
How can I do, using MySQL, to retrieve the addition of all the Qty values that corresponds to the same month and place that info on a view?

SELECT sum(Qty) as sum, month(date) as month, year(date) as year FROM table_name GROUP BY month(date), year(date)
will return
sum month year
4 12 2015
10 12 2016

What you probably want could be one of the following queries:
SELECT YEAR(`Date`) AS yr, MONTH(`Date`) AS mnt, SUM(Qty) AS Qty
FROM `table1`
GROUP BY YEAR(`Date`), MONTH(`Date`)
or
SELECT EXTRACT(YEAR_MONTH FROM `Date`) AS mnt, SUM(Qty) as Qty
FROM `table1`
GROUP BY EXTRACT(YEAR_MONTH FROM `Date`)
This query should produce something like this:
mnt | qty
---------+-----
2016-09 | 7
2016-10 | 6
The MySQL function EXTRACT() is able to return only some components of a date.

Related

Group by dates using a different table date range

I am trying to get the sum of costs by date, if the date lies between the start and end date which is coming in from a different table.
To explain:
Table 1
id
date
cost
1
2020-02-02
$10
2
2020-03-05
$100
1
2020-03-10
$200
3
2020-07-16
$200
1
2019-01-01
$50
1
2019-02-10
$50
3
2012-10-01
$500
Table 2
id
start_date
end_date
1
2020-01-01
2020-12-31
3
2020-01-01
2020-11-15
2
2020-01-01
2021-01-31
I just want to aggregate the costs by month only if the date in table 1 lies within the start and end date of table 2. So here I want the output to look like:
date
cost
2020-01-31
$10
2020-02-29
$10
2020-03-31
$300
2020-04-30
-
2020-05-31
-
2020-06-30
-
2020-07-31
$200
2020-08-31
-
2019-09-30
-
2019-10-31
$500
2019-11-30
-
2019-12-31
-
What I have tried so far:
select sum(cost), last_day(date)
from table1 inner join table2
on table1.id=table2.id
and table1.date>=table2.start_date and table1.date<=table2.end_date
group by last_day(date)
select year(date) as year,month(date) AS month, sum(cost) AS cost
from table1 inner join table2
on table1.id=table2.id
and table1.date>=table2.start_date and table1.date<=table2.end_date
group by table1.id, year(date), month(date)
order by last_day(date)

How to select from mysql that group by month in one year and sum value in a month?

received_amount | updated_at
----------------------------
4000 | 2020-11-02
2000 | 2020-11-03
1000 | 2020-12-02
1000 | 2020-12-04
3000 | 2021-01-02
2000 | 2020-01-02
This is my database table named "payment" structured and data on it.
Now I want to display something like below mentioned table data in my php web page when user select the year from calendar (I've use Zebra date picker for this). Zebra date picker gives the year.
In this scenario user select the year 2020 and it's should show the values as below.
Year | Month | Monthly Total
----------------------------
2020 | November| 6000
2020 | December| 2000
You can do:
select year, month, monthly_total
from (
select
extract(year from updated_at) as year,
extract(month from updated_at) as m,
monthname(updated_at) as month,
sum(receive_amount) as monthly_total
from t
where extract(year from updated_at) = 2020
group by year, month, m
) x
order by year, m
Result:
year month monthly_total
----- --------- -------------
2020 January 2000
2020 November 6000
2020 December 2000
See running example at DB Fiddle.
you can use group by like this in your query
GROUP BY EXTRACT(YEAR FROM updated_at), EXTRACT(MONTH FROM updated_at)
and then select like this
SELECT EXTRACT(YEAR FROM updated_at) as Year,
EXTRACT(MONTH FROM updated_at) as Month,
SUM(received_amount) as Monthly_Total

Group by month with date time in sql

halo i want to count the data and group by month (month start date) then show in date time, any idea ?
table_a
timestamp
2020-11-28 04:00:00
2020-11-28 05:00:00
2020-12-29 01:00:00
2020-12-29 02:00:00
2020-12-29 03:00:00
expected result:
timestamp count
2020-11-01 00:00:00 2
2020-12-01 00:00:00 3
my query is:
SELECT STR_TO_DATE(DATE_FORMAT(timestamp, '%Y-%m-dd HH'), '%Y-%m-dd HH'), count(*) as count
from table_a
but my results was:
timestamp count
2020-11-00 2
2020-12-00 3
You can use:
select str_to_date(concat_ws('-', year(timestamp), month(timestamp), 1)) as yyyymm,
count(*)
from table_a
group by yyyymm;
i fixed the question with add group by month(timestamp) , i m ignored the minuted, the important things is group by month , here are my full query :
SELECT timestamp, count(*) as count
from table_a
group by month(timestamp)
then the output show :
timestamp count
2020-11-01 04:00:00 2
2020-12-01 01:00:00 3

get total amount of expenses per month in MySQL

I am currently working with 2 tables, expenses and income. To keep the structure simple and can see it, this is the fiddle: http://sqlfiddle.com/#!9/256cd64/2
The result I need from my query is the total amount for each month of the current year, for this and tried something like this:
select sum(e.amount) as expense, DATE_FORMAT(e.date,'%m') as month
from expenses e
where year(e.date) = 2019
group by month
My problem with this is that it only takes me the months where there was registration and I would like it to take 12 months whether or not they have a registration, in the case that they did not return 0 as a total amount.
At the moment I am working with the table of expenses but I would like to have a single query that returns the monthly expenses and income, this is an example of the final output that I would like to obtain:
| Month | Expense| Incomes |
|---------|--------|---------|
| 01| 0 | 0 |
| 02| 3000 | 4000 |
| 03| 1500 | 5430 |
| 04| 2430 | 2000 |
| 05| 2430 | 1000 |
| 06| 2430 | 1340 |
| 07| 0 | 5500 |
| 08| 2430 | 2000 |
| 09| 1230 | 2000 |
| 10| 8730 | 2000 |
| 11| 2430 | 2000 |
| 12| 6540 | 2000 |
You need to generate the month values and then use left join to match to expenses:
select coalesce(sum(e.amount), 0) as expense, m.month
from (select '01' as month union all
select '02' as month union all
select '03' as month union all
select '04' as month union all
select '05' as month union all
select '06' as month union all
select '07' as month union all
select '08' as month union all
select '09' as month union all
select '10' as month union all
select '11' as month union all
select '12' as month
) m left join
expenses e
on year(e.date) = 2019 and
DATE_FORMAT(e.date,'%m') = m.month
group by m.month;
Here is a db<>fiddle.
As for income, you should ask another question about that.
You can use MONTH to get month value from your date column and then GROUP BY them to get your desired output as below-
SELECT SUM(e.amount) AS expense,
MONTH(e.date) AS month
FROM expenses e
WHERE YEAR(e.date) = 2019
GROUP BY MONTH(e.date)
Try changing your sum(e.amount) as expense to: COALESCE(sum(e.amount),0) as expense
The COALESCE function returns the first non NULL value.
SELECT
t1.month,
COALESCE(t2.amount, 0) AS expenses,
COALESCE(t3.amount, 0) AS incomes
FROM
(
SELECT 1 AS month UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12
) t1
LEFT JOIN
(
SELECT MONTH(date) AS month, SUM(amount) AS amount
FROM expenses
GROUP BY MONTH(date)
) t2
ON t1.month = t2.month
LEFT JOIN
(
SELECT MONTH(date) AS month, SUM(amount) AS amount
FROM incomes
GROUP BY MONTH(date)
) t3
ON t1.month = t3.month
ORDER BY
t1.month;

Get last row for given dates if date doesn't exist

How can we query a set of records to get data for particular dates where there might be gaps in dates.
Example Data
date | Price
------------------
2018-03-31 | 115
2018-03-29 | 114
2018-03-28 | 113
...
2017-03-29 | 117
2017-03-28 | 118
...
2016-12-30 | 143
2016-12-29 | 140
...
2015-12-31 | 110
2015-12-30 | 111
Required Data for dates: 2018-03-31, 2017-03-31, 2016-12-31, 2015-12-31
date | Price
------------------
2018-03-31 | 115
2017-03-31 | 117
2016-12-31 | 143
2015-12-31 | 110
You can do this with correlated sub query. The following will return the price for the exact date or the closest prior date:
SELECT dates.dt, (
SELECT price
FROM t
WHERE date <= dates.dt
ORDER BY date DESC
LIMIT 1
) AS price
FROM (
SELECT '2018-03-31' AS dt UNION ALL
SELECT '2017-03-31' UNION ALL
SELECT '2016-12-31' UNION ALL
SELECT '2015-12-31'
) AS dates
Demo on db<>fiddle
You can use MySQL Last day function and date_format to achieve the desired result. Check out the query :-
select last_day(a11.d_date), a11.price
from test a11
join
(select MAX(d_date) d_date, DATE_FORMAT(d_date, "%M %Y")
from test
group by DATE_FORMAT(d_date, "%M %Y")
) a12
on a11.d_date = a12.d_date
SQL Fiddle
seem you need the max price for year
select max(date), max(price)
from my_table m
inner join (
select max(date), year(date) my_year
from my_table
group by year(date)
) t on t.my_year = year(m.date)
group by year(m.date)