Select totals from database table based on month - php

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)

Related

query to calculate the total salary for each month

I have a table with 4 fields as follows
I want an Sql query to calculate a total salary for each month, where the result will be as follows
Do you just want group by and sum()?
select sum(salary) salary, month
from mytable
group by month

Fetching sum of amount for a specific year from mysql

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'))

Mysql Multiple queries to calculate values for month

first of all i want to extract dates of the relevant month from the database.for that i have used following query and i had generated the following output.
code
SELECT DISTINCT date as tot FROM attendance WHERE date LIKE '%2016-06%'
output
then i need to get the present employees for that dates from the database.but when i try that, it gives me total for one date only.how can i over come this.Here is my code.
SELECT DISTINCT date as days, COUNT(DISTINCT employee_id) as tot FROM attendance WHERE in_time != '' AND out_time != '' AND date LIKE '%2016-06%'
You need to use group by. mysql just returns an arbitrary value when you omit fields from the select list that are not included in aggregate functions. Then you can remove distinct:
SELECT date as days, COUNT(DISTINCT employee_id) as tot
FROM attendance
WHERE in_time != '' AND out_time != '' AND date LIKE '%2016-06%'
GROUP BY date
Per comment, what data type is your date field? Assuming it's a date, you could remove like and use year and month:
AND Month(date) = 6 AND Year(date) = 2016

select month date from database

I have this code to select distinct value and give me the number of values.
I want to select only the value in this month.
Example : give me all value of month 06 only in the same way distinct value number only in the month 06 or 05
Remarque: the date type in the database is varchar not date.
$sql=mysqli_query($conn,"select db_shopname, COUNT(db_shopname) as num FROM
(select tbl_order.db_shopname from tbl_order union all select tbl_item.db_shopname from tbl_item)t
GROUP BY db_shopname ")or die(mysqli_error($conn));
while($res=mysqli_fetch_array($sql)){
echo $res['db_shopname'];echo" ";echo"(";echo $res['num'];echo")";echo"<br/>";
}
To pull the month out of the varchar field use:
MONTH(STR_TO_DATE(db_date ,'%Y/%m%d'))=MONTH(NOW())
Add this into where condition
EDIT:
I don't know its work for what you expecting but try it .
select db_shopname, COUNT(db_shopname) as num
FROM
(select tbl_order.db_shopname
from
tbl_order
where MONTH(STR_TO_DATE(`db_date` ,'%Y/%m%d'))=MONTH(NOW())
union
all
select tbl_item.db_shopname
from tbl_item
where MONTH(STR_TO_DATE(`db_date` ,'%Y/%m%d'))=MONTH(NOW()))t
GROUP BY db_shopname
hi if you want to convert date varchar to date for searching you can use below example
select date_format(str_to_date('31/12/2010', '%d/%m/%Y'), '%Y-%m-%d') - return 2010-12-31
If you want only year
select date_format(str_to_date('31/12/2010', '%d/%m/%Y'), '%Y') - return 2010
If you want only month
select date_format(str_to_date('31/12/2010', '%d/%m/%Y'), '%m') - return 12
If you want only day
select date_format(str_to_date('31/12/2010', '%d/%m/%Y'), '%d') - return 31
Example
select count(name) from test where MONTH(str_to_date(date, '%d/%m/%Y')) = 06 group by name

to show total record in wanted date row

I retrieved data from my table by summing result and show in table.I want to show sum result record between date='2014-01' and date='2014-04' in 2014-04 column name.And then,I want to retrieve other sum result in 2014-05 to current date.
How can I do that?
SELECT MONTH(regist_timestamp) AS Month, YEAR(regist_timestamp) AS Year,
count(id) AS numberOfVisits FROM ash_members WHERE
DATE(regist_timestamp) < CURDATE() GROUP BY
DATE_FORMAT(`regist_timestamp`,'%Y/%m' )
Format the date on SELECT then GROUP BY. Try with -
SELECT MONTH(regist_timestamp) AS Month, YEAR(regist_timestamp) AS Year,
count(id) AS numberOfVisits, DATE_FORMAT(`regist_timestamp`,'%Y/%m' ) formatted_date
FROM ash_members
WHERE DATE(regist_timestamp) < CURDATE()
GROUP BY formatted_date

Categories