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.
Related
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}')
)";
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
...
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.
Going to make a line graph. I want to query count with difference of 5 days.
Suppose if I have following rows:
booking_date
2015-02-1
2015-02-3
2015-02-5
2015-02-6
2015-02-6
2015-02-9
2015-02-10
2015-02-15
2015-02-17
2015-02-23
2015-02-28
In above table column it contains date. Now How can I do mysql query so that it can return with difference of 5 days like:
1 => 3 // count of date between 2015-02-1 & 2015-02-05 is 3
2 => 4 // count of date between 2015-02-06 & 2015-02-10 is 4
3 => 1 // count of date between 2015-02-11 & 2015-02-15 is 1
4 => 1 // count of date between 2015-02-16 & 2015-02-20 is 1
5 => 1 // count of date between 2015-02-21 & 2015-02-25 is 1
6 => 1 // count of date between 2015-02-26 & 2015-02-30 is 1
Is any direct way to query like above. I am not so good at mysql. But can do php nicely.
You can do the following to get everything in the same query.
First of all get the unix timestamp for the date you want to start grouping in 5 days. In your example that would be 2015-02-01 -> 1422748800
Then the query would be the following:
SELECT COUNT(*), FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5)) as FiveDayPackNumber from tbl GROUP BY FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5))
Haven't tested it so it may require some tweaking but you can get the idea: It will group them by the number of 5-days-packs that passed since your initial date, starting at 0.
Do you mean to do something like:
SELECT COUNT(1) FROM {table_name} WHERE booking_date > 2015-02-01 AND booking_date < 2015-02-05
?
(completely off memory so you might need to slightly alter it)
SELECT count(*)
FROM tbl
WHERE booking_date >= start_date
AND booking_date <= end_date
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")