Fetching sum of amount for a specific year from mysql - php

I have the following table with dummy values in mysql database:
id cnic amount depositDate receivedBy receivingZone remarks
1 11111 10000 01-Nov-2019 11111 1 Ok
2 11111 10000 07-Nov-2019 11111 1 ok
Now i want to get the sum of amount from the table for specific year (2019 in this case) where the year came from current timestamp (it may be 2020, 2021 etc depends on the current date)
Any help plz

You can use the YEAR function to get the year of the depositDate column and also the current year and then sum only the values which match:
SELECT SUM(amount) AS amount
FROM yourtable
WHERE YEAR(STR_TO_DATE(depositDate, '%d-%b-%Y')) = YEAR(CURDATE())

You can try below -
select sum(amount)
from tablename
where year(depositdate)=year(now())

I would write the WHERE clause to be sargable:
SELECT SUM(amount)
FROM yourTable
WHERE depositDate >= DATE_FORMAT(NOW() ,'%Y-01-01') AND
depositDate < DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 1 YEAR) ,'%Y-01-01');
This approach, while a bit more verbose than the other answers which use the YEAR() function, would allow an index on the depositDate column to be used.

Based on your sample year, we need to recognize first the date using str_to_date
select sum(amount)
from tableA
where year(now()) = year(str_to_date(depositdate, '%d-%b-%Y'))

Related

Get daily average of values of a month

I want to get the daily average of the values from the sensor 'temp'.
So for example if I select November it should show me:
2019-11-01 = <avg value>
2019-11-02 = <avg value>
2019-11-03 = <avg value>
...and so on
So how can I get the average of my values daily? I hope you can understand this.
Sample data:
Try this :
SELECT AVG(value) FROM your_table WHERE MONTH(time) = 7
AVG will return the average value of your column value and you use a condition with MONTH(time) = the_month_you_want. For example, MONTH('1802-07-07') will return 7, so adapt the query according to the month you want.
The expected result seem to suggest that you want data for November 2019. You need to group by date part only:
SELECT CAST(time AS date), AVG(value)
FROM t
WHERE time >= '2019-11-01'
AND time < '2019-11-01' + INTERVAL 1 MONTH
GROUP BY CAST(time AS date)

Select totals from database table based on month

I have a table which has product quantity column and the database has multiple entries in a single month.
Date format is (YYYY-MM-DD)
Date Quantity
2016-03-01 1200
2016-03-05 200
2016-04-05 500
2016-04-10 1000
2016-05-05 850
2016-05-10 50
So I want data as:
March (2016-03-01 to 2016-03-31) = 1400
April (2016-04-01 to 2016-04-30) = 1500
May (2016-05-01 to 2016-05-31) = 900
How can I do this?
Use the following query and it will return the result that you want.
' date field ' is the name of the column where date is inserted and Tablename is the name of the Table.
SELECT MONTH(date field) as month , YEAR(date field) as year , SUM(quantity) as
quantity FROM Tablename GROUP BY MONTH( date field )
select MONTH(Date) As dt , SUM(quantity) total from #Table1
GROUP BY MONTH(Date)
You can use DATE_FORMAT to convert the date field into just the year-month format, select the SUM of the quantity field, and then GROUP_BY the year-month field.
SELECT DATE_FORMAT('%Y-%m', `date`) as `ym`, SUM(`quantity`) FROM `table` GROUP BY `ym` ORDER BY `ym` ASC
You can try grouping by the month and year, taking the sum of the quantity field as an aggregate.
SELECT MONTH(Date), YEAR(Date), SUM(quantity)
FROM yourTable
GROUP BY MONTH(Date), YEAR(Date)
If you want the fancy output with actual date ranges, that would be a bit more work to do. Those dates may not exist in your original data set, so it could require a date table. And handling February in a leap year could be a real pain.
Use the following query same as it is it will return what you want
SELECT CONCAT(DATE_FORMAT(date,'%b'),' ',
(SELECT CONCAT('(',YEAR(date),'-',LPAD(MONTH(date),2,0),'-01 - ')
), (SELECT concat(last_day(date),')')
)) dates, SUM(quantity) qty FROM DATES GROUP BY MONTH(date) ORDER BY MONTH(date)

Working out the amount of free dates in a given time period

I have a fun one for you. I have a database with the date columns free_from and free_until. What I need to find is the amount of days between now and 1 month today which are free. For example, if the current date was 2013/01/15 and the columns were as follows:
free_from | free_until
2013/01/12| 2013/01/17
2013/01/22| 2013/01/26
2013/01/29| 2013/02/04
2013/02/09| 2013/02/11
2013/02/14| 2013/02/17
2013/02/19| 2013/02/30
The answer would be 16
as 2 + 4 + 6 + 2 + 2 + 0 = 16
The first row only starts counting at the 15th rather than the 12th
since the 15th is the current date.
The last row is discounted because none of the dates are within a
month of the current date.
The dates must be counted as it the free_from date is inclusive and
the free_until date is exclusive.
I'm assuming DATEDIFF() will be used somewhere along the line, but I can't, for the life of me, work this one out.
Thanks for your time!
Edit: This is going into PHP mysql_query so that might restrict you a little concerning what you can do with MYSQL.
SET #today = "2013-01-15";
SET #nextm = DATE_ADD(#today, INTERVAL 1 month);
SET #lastd = DATE_ADD(#nextm, INTERVAL 1 day);
SELECT
DATEDIFF(
IF(#lastd> free_until, free_until, #lastd),
IF(#today > free_from, #today, free_from)
)
FROM `test`
WHERE free_until >= #today AND free_from < #nextm
That should work. At least for your test data. But what day is 2013/02/30? :-)
Dont forget to change #today = CURDATE();
The best I can think of is something like:
WHERE free_until > CURDATE()
AND free_from < CURDATE() + INTERVAL '1' MONTH
That will get rid of any unnecessary rows. Then on the first row do in PHP:
date_diff(date(), free_until)
On the last row, do:
date_diff(free_from, strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month"))
Then on intermediate dates do:
date_diff(free_from, free_until)
Something to that effect, but this seems extremely clunky and convoluted...
From the top of my mind... first do a:
SELECT a.free_from AS a_from, a.free_until AS a_until, b.free_from AS b_from
FROM availability a
INNER JOIN availability b ON b.free_from > a.free_until
ORDER BY a_from, b_from
This probably will return a set of rows where for each row interval you have next i.e. greater intervals. The results are ordered strategically. You can then wrap the results in a partial group by:
SELECT * FROM (
SELECT a.free_from AS a_from, a.free_until AS a_until, b.free_from AS b_from
FROM availability a
INNER JOIN availability b ON b.free_from > a.free_until
ORDER BY a_from, b_from
) AS NextInterval
GROUP BY a_from, b_until
In the above query, add a DATE_DIFF clause (wrap it in SUM() if necessary):
DATE_DIFF(b_until, a_from)

MySQL BETWEEN DATE RANGE

I have a scenario where I need to pull up delivery dates based on a table below (Example)
job_id | delivery_date
1 | 2013-01-12
2 | 2013-01-25
3 | 2013-02-15
What I'm trying to do is show the user all the delivery dates that start with the earliest (in this case it would be 2013-01-12) and add an another 21 days to that. Basically, the output I would expect it to show of course, the earliest date being the starting date 2013-01-12 and 2013-01-25. The dates past the February date are of no importance since they're not in my 21 date range. If it were a 5 day range, for example, then of course 2013-01-25 would not be included and only the earliest date would appear.
Here is main SQL clause I have which only shows jobs starting this year forward:
SELECT date, delivery_date
FROM `job_sheet`
WHERE print_status IS NULL
AND job_sheet.date>'2013-01-01'
Is it possible to accomplish this with 1 SQL query, or must I go with a mix of PHP as well?
You can use the following:
select *
from job_sheet
where print_status IS NULL
and delivery_date >= (select min(delivery_date)
from job_sheet)
and delivery_date <= (select date_add(min(delivery_date), interval 21 day)
from job_sheet)
See SQL Fiddle with Demo
If you are worried about the dates not being correct, if you use a query then it might be best to pass in the start date to your query, then add 21 days to get the end date. Similar to this:
set #a='2013-01-01';
select *
from job_sheet
where delivery_date >= #a
and delivery_date <= date_add(#a, interval 21 day)
See SQL Fiddle with Demo
SELECT date,
delivery_date
FROM job_sheet
WHERE print_status IS NULL
AND job_sheet.date BETWEEN (SELECT MIN(date) FROM job_sheet) AND
(SELECT MIN(date) FROM job_sheet) + INTERVAL 21 DAY
SELECT j.job_id
, j.delivery_date
FROM `job_sheet` j
JOIN ( SELECT MIN(d.delivery_date) AS earliest_date
FROM `job_sheet` d
WHERE d.delivery_date >= '2013-01-01'
) e
ON j.delivery_date >= e.earliest_date
AND j.delivery_date < DATE_ADD(e.earliest_date, INTERVAL 22 DAY)
AND j.print_status IS NULL
ORDER BY j.delivery_date
(The original query has a predicate on job_sheet.date; the query above references the d.delivery_date... change that if it is supposed to be referencing the date column instaed.)
If the intent is to only show delivery_date values from today forward, then change the literal '2013-01-01' to an expression that returns the current date, e.g. DATE(NOW())

how to group MySql rows based on month?

I've a table with a datetime (format: 'Y-m-d H:i:s') 'created' field and 'amount' (integer) field in each row. Now I want to find out month wise total 'amount' in last year. How can I do this?
EDIT
I made an edit to clarify the actual problem. so basically I want to know total 'amount' in each month, but only for the last year.
EDIT2
Last year means last 365 days. So somewhere I've to consider 'current day'?
EDIT3
My bad, actually last year is last 12 months. So number of days would be between 336 and 365.
Try this (updated to answer your "edit3"):
SELECT
YEAR(created) as year,
MONTH(created) AS month,
SUM(amount) AS total_amount
FROM table1
WHERE created
BETWEEN DATE(NOW()) - INTERVAL (DAY(NOW()) - 1) DAY - INTERVAL 11 MONTH
AND NOW()
GROUP BY YEAR(created), MONTH(created)
ORDER BY YEAR(created), MONTH(created);
Example result (when run in April 2010):
year month total_amount
2009 5 26
2010 1 20
Note also that months with no entries will not be returned at all (rather than being returned with total_amount = 0).
Test data:
CREATE TABLE table1 (created datetime NOT NULL, amount INT NOT NULL);
INSERT INTO table1 (created, amount) VALUES
('2010-01-01 13:56:23', 5),
('2010-01-04 13:56:23', 15),
('2009-05-04 13:56:23', 26);
This returns the count and total amount for last year:
SELECT MONTH(created) as month_updated,
COUNT(created) as month_updates, SUM(amount) as month_total FROM table
WHERE created BETWEEN DATE_ADD(NOW(), INTERVAL -1 YEAR) AND NOW()
GROUP BY MONTH(created)
Or, if you specifically mean just 2009:
SELECT MONTH(created) as month_updated,
COUNT(created) as month_updates, SUM(amount) as month_total FROM table
WHERE created BETWEEN '2009-01-01 00:00:00' AND '2009-12-31 23:59:59'
GROUP BY MONTH(created)
SELECT count(some_row) AS or_whatever FROM your_table GROUP BY MONTH(update);
To be more specific (with your update):
SELECT SUM(amount) FROM table_name GROUP BY MONTH(created);

Categories