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
...
Related
I am working on Payroll System where an employee can be deployed on projects from any_date to any_date. Sample MySQL table below
id empid project_id start_date end_date
1 1 2 2016-11-05 15:10:22 2016-12-11 15:11:21
2 1 3 2016-12-13 15:26:10 2016-12-20 15:29:40
3 1 2 2016-12-23 15:31:46 2017-01-18 15:32:35
Now, if I want to calculate the number of days in given month worked on all projects, I am using Eloquent which translates to below query(I verified in DB::getQueryLog()):
SELECT
*
FROM
payroll_project_tracks
WHERE
empid = 1
AND '2016-12-01 00:00:00' BETWEEN start_date AND end_date
AND '2016-12-31 23:59:59' BETWEEN start_date AND end_date
After getting the rows that have deployments of given month (month = Dec 2016 here), I want to calculate Number of Days worked on all projects in a given month.
I am getting no results with the above query. Can anyone correct me or let me know if there is some better way of doing this.
I think that the query should be:
SELECT *
FROM payroll_project_tracks
WHERE
empid = 1 AND
start_date <= '2016-12-31 23:59:59' AND
end_date >= '2016-12-01 00:00:00'
This query will return the 3 rows you provided, but will cut out the following:
4 1 2 2017-01-01 15:31:46 2017-01-18 15:32:35
is this ok?
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 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.
I have to filter data for last 3 months from current date, so that would be to fetch data from Aug to Oct. But value exist for October only in mysql table, so now i want to display record in following format:
Month Values
Aug 0
Sept 0
Oct 10
But my query only shows October month records as i dont have record for previous 2 months.
How can i do this. Following is my query.
SELECT
CASE WHEN COUNT(DISTINCT(user_analytics_id)) > 0 THEN COUNT(DISTINCT(user_analytics_id)) ELSE 0 END as pic_views,
YEAR(user_profile_viewed_date) AS pic_viewed_year,
MONTHNAME(user_profile_viewed_date) as pic_viewed_month
FROM (`user_analytics`)
WHERE `user_id` = '1' AND `view_type` = 'picture'
AND `user_profile_viewed_date` BETWEEN '2010-07-28 04:23:56' AND '2010-10-28 04:23:56'
GROUP BY MONTH(user_profile_viewed_date) ORDER BY MONTH(user_profile_viewed_date) ASC
The above query is not working as i want it to. So pls help me on this..
Create another table with numbers 1 .. 12 and LEFT JOIN your table to it.