How to get the sum of values by month vice using mysql - php

I would like to get sum of total price from the below tables by month from to month to .Example : Jan to Mar How to get the name of the month as well sum of the particular month total. I totally confused with this.Anyone please help me. I'm passing the input like '04-2014' to '05-2014'
Sale table
id_sale date time
1 2014-05-05 12.30 am
2 2014-05-06 10.30 am
3 2014-05-25 12.30 am
Sale Product table
id_sale_product id_sale price quantity id_product
1 1 10.00 1 1
2 1 20.00 1 2
3 2 20.00 3 5
4 3 20.00 4 6
I want to filter by date in sale table and get the sum of the price * quantity = total for every date ie,2014-05-05,2014-05-06..etc
I have tried below query
$query = 'SELECT sp.`id_product_type`,sp.`id_product`,sp.`quantity`,sp.`price`,s.`date`,SUM(sp.`price` * sp.`quantity`) AS total
FROM `'._DB_PREFIX_.'sale_product` sp
LEFT JOIN '._DB_PREFIX_.'sales s ON (sp.id_sale = s.id_sale)
WHERE DATE_FORMAT(s.`date`, "%m-%Y") BETWEEN "'.$monthFrom.'" AND "'.$monthTo.'"';
The output should be :
Example: Month
January Rs. 2,00,000
Feburary Rs. 2,20,000

This query should help:
SELECT DATE_FORMAT(s.date, "%M-%Y"),SUM(sp.`price` * sp.`quantity`) AS total
FROM `'._DB_PREFIX_.'sale_product` sp
LEFT JOIN '._DB_PREFIX_.'sales s ON (sp.id_sale = s.id_sale)
WHERE DATE_FORMAT(s.`date`, "%m-%Y") BETWEEN "'.$monthFrom.'" AND "'.$monthTo.'"';
GROUP BY DATE_FORMAT(s.date, "%M-%Y")

Related

Is it possible to loop the result of a query that gets each revenue for the last 8 weeks based on the input date of the user?

User input = '2017-03-12'
Let say I have this tableRevenue
date revenue
---------- ---------
2017-01-01 100
2017-01-08 100
2017-01-15 100
2017-01-22 100
2017-01-29 100
2017-01-05 100
2017-01-12 100
2017-02-19 100
2017-02-26 100
2017-03-05 100
2017-03-12 100
And another tableHolidays which contains
date
----------
2017-01-15
2017-02-19
2017-03-05
I want to display it like this:
date revenue
---------- ---------
2017-01-01 100
2017-01-08 100
2017-01-22 100
2017-01-29 100
2017-01-05 100
2017-01-12 100
2017-02-26 100
2017-03-12 100
I want to display the revenue each of the last 8 weeks and I want to skip all the dates that are existing in tableHolidays using a loop. Is this possible in PHP?
mention: you didn't tag any specific database - my answer will refer to SQL-Server:
assuming #UserDate is a variable with the user input date
Use Date-Functions (specific to every DB system) to calculate the date range. in your case to subtract the 8 weeks.
Select all rows within this date range
exclude (NOT IN) all dates from your resultset which occur in your tableHolidays table
GROUP BY weeks (calculate weeknumber with WEEK) and SUM the revenue
Query:
SELECT WEEK(tR.date) as weeknr
,SUM(tR.revenue)
FROM tableRevenue tR
WHERE tR.date >= DATEADD(wk,-8,#UserDate)
AND tR.date <= #UserDate
AND tR.date NOT IN (SELECT date FROM tableHolidays)
GROUP BY WEEK(tR.date)
you can use the 'ANY' which is a mysql operator
for more information you can visit this link
https://www.w3schools.com/sql/sql_any_all.asp
$userInput = '2017-03-12';
$sql = "SELECT `date`, `revenue`
FROM `tableRevenue`
WHERE (
(`date` = ANY
(SELECT `date` FROM `tableHolidays` WHERE DATE(date)<='{$userInput}'))
AND (DATE(date) <='{$userInput}')
)";

How can i get all months With between two dates

i have this query that get all regsiterd data between this two dates
SELECT date as date_month, status FROM tbl_reports WHERE status != 0 AND (date BETWEEN '2017-01-01' AND '2017-12-31') GROUP BY MONTH(date)
result
date_month
2017-04-19
2017-05-24
2017-06-26
2017-07-01
but how i can get the result even if there is no regsitered input in the other months
want result
Date_month
2017-01-01
2017-02-01
2017-03-01 - it will give this value if there is no such data in this month
2017-04-19 - i will get the input date from database
'' '' ''
2017-12-31
is it possible ? or i need some php code to manipulate this data?
thank you in advance for my answering my question.. :)
Create an extra table, named datehelper. Provide it with 2 columns: year and month. And fill those with year: 2017 until 2027 and 1 / 12 for the months.
SELECT
datehelper.year,
datehelper.month,
r.status
FROM datehelper
LEFT JOIN tbl_reports r ON MONTH(r.date) = datehelper.month
AND YEAR(r.date) = datehelper.year AND status != 0
WHERE (datehelper.year = '2017')
ORDER BY datehelper.year, datehelper.month
I see you would get duplicates on the left joined part if there are more records there in a certain month.
What status would you expect? Let's think there are records with status 0 (excluded) status = 1, and status = 2
datehelper:
year month
2017 1
2017 2
2017 3
2017 4
...
2017 11
2017 12
2018 1
...

MySQL Two Queries Producing One Value

I need to make a query that selects from two different tables. Basically, I only want to select the rows from the dates table that have no pending orders in the orders table.
For example, the dates table has the values of July 1, July 2 and July 3. July 2 has orders with the status = PEN in the orders table so the table will only show July 1 and July 3.
Query 1 for dates:
$sql = "SELECT * FROM dates WHERE DATEDIFF(CURDATE(), date) >= 30 AND `30day`='No'";
I have yet to build a query for the orders table as I am sure this needs to be integrated into one query together, and I am not sure what to do.
I know you can do two SELECT queries in one, and I am aware of how to do this, but I am unsure of how to cause the second SELECT query to be affected by the first SELECT query.
dates database has columns id date closed 30day 60day
orders database has columns id date order status
I need this query to flag any orders with the statuses PEN BO FBO.
Thank you in advance!
Sample Data:
dates table:
Date - 30-day Value
July 1 - No
July 2 - No
July 3 - No
orders table:
Date - Orders - Status
July 1 - 7123456 - PEN
July 1 - 7123457 - SHI
July 1 - 7123487 - SHI
July 2 - 7256789 - SHI
July 2 - 7256790 - SHI
July 2 - 7256791 - SHI
July 3 - 7215368 - SHI
July 3 - 7125369 - SHI
July 3 - 7659876 - BO
July 4 - 7569235 - FBO
July 4 - 7986585 - FBO
Expected Result:
Date
July 2
July 3
Omitted Dates:
Date - Reason
July 1 - because there is an open order
July 4 - because there is an open order
I don't want an omitted table - just wanted to show what wouldn't show up.
could be this what you are looking for (i don't know your schema so for the join i have used a column named key
$sql = "SELECT *
FROM dates
LEFT JOIN orders on (dates.date = orders.date and orders.status not in ('PEN','BO', 'FBO'))
WHERE DATEDIFF(CURDATE(), dates.date) >= 30
AND `30day`='No'";
otherwise if you need that status is not PEN, BO and FBO then you can
..
$sql = "SELECT *
FROM dates
LEFT JOIN orders ON dates.date = orders.date
WHERE DATEDIFF(CURDATE(), dates.date) >= 30
AND `30day`='No'
AND orders.status NOT IN ('PEN','BO', 'FBO' ) ";
..
$sql = "SELECT *
FROM dates
INNER JOIN orders ON dates.date = orders.date
WHERE DATEDIFF(CURDATE(), dates.date) >= 30
AND `30day`='No'
AND orders.status IN ('PEN','BO', 'FBO' ) ";
based on the sample provided this should return the rows you need
$sql = " SELECT *
FROM dates
WHERE DATEDIFF(CURDATE(), date) >= 30
AND `30day`='No'
AND date not in (
select date
from orders
where order.status IN ('PEN','BO', 'FBO' )
) ";

How do I know which month is not exist in a table date column from connection date (registered date)?

Well, I have 2 mysql tables :
1) clients
client_id conn_date monthly_bill
=========================================
10 2016-06-01 700.00
11 2016-08-30 650.00
12 2016-08-30 1000.00
13 2016-07-01 700.00
2) clients_pay_bill
cbp_id client_id bill_month
===============================
1 10 2016-08-30
2 11 2016-08-12
3 12 2016-08-08
In clients table conn_date column is the date when user first register or connection date.
In clients_pay_bill table bill_month column shows that which month client pay his bill.
Now in a current month I want to show the total due bill from connection date to current date.
For ex : In clients_pay_bill table clients_id = 10 is only paid this 2016-08-30 month bill. But didn't paid this 2016-06-08 and 2016-07-08 months bill. So his due is now = 1400 because his monthly bill is 700.
I can't imagine how the mysql query should look like ! How can I do this ?
Update :
select C.client_id,C.monthly_bill,
(
PERIOD_DIFF(EXTRACT(YEAR_MONTH from CURDATE()),
EXTRACT(YEAR_MONTH from C.conn_date))+1
-
count(P.bill_month)
)*C.monthly_bill as Need_many
from clients C
left join clients_pay_bill P
on P.client_id=C.client_id and P.bill_month<date_format(curdate(),'%Y-%m-01')
group by C.client_id
Function PERIOD_DIFF() return the number of months between periods (current month and connected date). Count() rows in clients_pay_bill return count of payd month. The difference between these values is the count of unpaid months.

fetch daily customer flow data

I am trying to fetch daily customer flow from table 'tbl_transaction' using the following query:
SELECT DAY(date_time) AS date, SUM(members) AS customers FROM tbl_transaction WHERE MONTH(date_time)='.$mon.' GROUP BY "date_time"
But it is generating this dataset which is not grouped according to date:
date | customers
11 3
12 2
13 1
14 2
14 3
15 7
16 4
17 3
17 2
17 7
17 2
18 5
18 5
18 4
19 2
How to show the dates and no of customers as on particular date.
Your query should look like
SELECT DAY(date_time) AS date,
SUM(members) AS customers
FROM tbl_transaction
WHERE MONTH(date_time)='03' GROUP BY DATE(date_time)
Since date_time is in Y-m-d H:i:s so doing Group by date_time will return all the rows.
Also if you do Group By date which is obtained as DAY(date_time) AS date yield wrong result.
DAY('2014-01-01') = 1
DAY('2014-02-01') = 1
So it will sum up the result for days irrespective of actual date.

Categories