I have a post_meta database which contains the following fields:
meta_id, post_id, meta_key, meta_value
I have created a view in PhpMyAdmin to isolate only those records with specific text to bring up a list of clients and appointment times. Each record is unique so that for a client with several appointments they would have the same name, but different time and ID. Name and time are contained in the meta_value column. Column view
Here is an example:
postID | appt_date | firstname | lastname
-----------------------------------------------
2106 | 1427698800 | Sam | Snead
2107 | 1428649200 | John | Miller
2108 | 1428476400 | Sam | Snead
My challenge is pulling both the number of appointments for the month to date, last month (e.g. Sept) and previous month ( e.g. Aug), as well as the total unique clients for the month. I am going to graph this so that the result is a column and line graph with Aug clients (column) and Aug appointments (line), sim for Sept. The MTD would be prev month plus whatever new clients and appointments so far in the month.
I not had much luck because the date is in Unix format and the month to date (which of course varies based on the day of the month). Additionally the clients show up multiple times (some of them have had 50 appointments) so selecting them only once is a challenge.
You can get this month and last month using some date() (on php side):
$this_month = date("Y-m-d");
$last_day_this_month = date("Y-m-t", strtotime($this_month));
$first_day_this_month = date("Y-m-01", strtotime($this_month));
$next_month = date("Y-m-01", strtotime($last_day_this_month." + 5 days"));
$last_month = date("Y-m-01", strtotime($first_day_this_month." - 5 days"));
Being in unix time just use date on the unix time and you will get the formatted time.
The rest is just doing some count on or group by clauses in the SQL part.
First of all you seem to want this :
you want the total number of appointments client A had in september, the same for august. Do i get that right ?
In that case you will need to figure out the right date/time to look at in your query you can do this by making use of an epoch converter such as (https://www.unixtimestamp.com/index.php)
When you found the correct epochs you can use those in your where i.e :
where appt_date < (highest epoch last day of month with time 00:00) and
appt_date > (last day of previous month time 00:00)
That isolates the time between the first and last day of the month you want to have.
For your second problem : as well as the total unique clients for the month. I would urge you to look at how to use the distinct or group by functions.
You can use the FROM_UNIXTIME() function to convert the unix timestamp to a date, and then use the MONTH() function to get the month number. Something like this:
SELECT COUNT(*) as number_of_appointments, MONTH(FROM_UNIXTIME(appt_date)) as number_month
FROM {your_view}
GROUP BY number_month
will give you the total number of appointments per month.
Related
I'm trying to grab SQL data via PHP with a tally for case types each week to display like so:
Week 1 | Date From | Volume
Week 2 | Date From | Volume
Week 3 | Date From | Volume
and so on... without having to manually for each week. I have week number variables set as the business Year starts in July, so Week 1 is the first week in July. Ideally I'd like to use the company weeks but will settle for start of normal year. I've started with this:
SELECT YEARWEEK(date) as weekNum, MIN(sr_mob.`date`) as start_date,
count(*) as numRecords
FROM sr_mob
WHERE outcome='Escalated'
GROUP BY YEARWEEK(date)
This gives me the return data, but the start_date varies depending on when first entry was that week.
Is there any way to define a week in PHP then query the table (which doesn't contain the week numbers) to get what I'm after? Or does this sound like I'll manually have to request each week...
I can run a single query with say:
$Week1 ($week1=20180731-7;)
I guess what I am looking for is a way of doing a for each or while, using the $week variable, without having to write out 52 variables, if that makes sense.
Using reference from: https://stackoverflow.com/a/30373395/2397717
SELECT
YEARWEEK(date) as weekNum,
STR_TO_DATE(CONCAT(YEARWEEK(date),' Monday'), '%X%V %W') as start_date,
count(*) as numRecords
FROM sr_mob
WHERE outcome='Escalated'
GROUP BY YEARWEEK(date)
This is the output of a query on a temporary table created in a complex stored procedure in MySQL :
The goal now is to have the average duration per day like we have here, but for each week by month
So I want to take every month and divide it into 4 set of days with the average duration per day.
Therefore no matter how many days there are in the month, I will have 4 values for it.
How can I do that ?
Note : If it is easier I can do it with php, since I will use the data with this language.
You have a query that groups per day, e.g.:
select
the_date as day,
sec_to_time(avg(timestampdiff(second, start_time, end_time))) as duration
from ...
group by the_date;
You want days 1-7, 8-14, 15-21, 22-end per month instead. Use CASE WHEN to build the groups.
select
year(the_date) as year,
month(the_date) as month,
case
when day(the_date) <= 7 then '01-07'
when day(the_date) <= 14 then '08-14'
when day(the_date) <= 21 then '15-21'
else '22-end'
end as day_range,
sec_to_time(avg(timestampdiff(second, start_time, end_time))) as duration
from ...
group by year, month, day_range
order by year, month, day_range;
I have an application that records user transactions into a database. The data recorded includes: the time a user performed a transaction (e.g. purchasing materials), the day (e.g. 2016-12-23), what the user transacted, etc. I have an admin panel that collects this data and shows the overall statistics. I need to analyse the data such that, for example; if i need to find out all purchases made for that week (i.e.from Sunday to the current day of the week), i select all appearances from the database that have the dates from the immediate previous Sunday until the current day, so that i show a weekly statistics.
Now, since the average is done weekly, i need only to get the current date, that of the previous sunday and the days in between. I know how to get these two but no idea how to get the middle dates depending on the day of the week.
Note that the time i huse is in the format
$today = date("Y-m-d");
and to get the previous sunday i use:
$prevsunday = date("Y-m-d",strtotime("last Sunday"));
I ask how to get the middle days, and how to handle the updated query.
Any suggestions to improve my query?
Assuming MySQL, you can use dayofweek to figure out "previous sunday":
mysql> select curdate(), dayofweek(curdate()),
curdate() - interval (dayofweek(curdate())-1) day AS sunday;
+------------+----------------------+------------+
| curdate() | dayofweek(curdate()) | sunday |
+------------+----------------------+------------+
| 2016-02-23 | 3 | 2016-02-21 |
+------------+----------------------+------------+
1 row in set (0.00 sec)
So you'd have something like
SELECT ...
...
WHERE dateofpurchase BETWEEN
(curdate() - interval (dayofweek(curdate())-1) day)
AND
curdate();
My table has the following columns
| customer_id | service_start_date |
I want to provide the service on alternate week days from the service start date (eg:- every other mondays, every other tuesdays etc..)
If the service_start_date is a monday, then the service will be delivered on every other mondays.
Is there any way to query the mysql table to get all customer_ids who needs service on a particular date?
Try something like
SELECT customer_id
FROM Table as t
WHERE MOD(DATEDIFF(DATE(NOW()), DATE(service_start_date)), 14) = 0
This is assuming that your service_start_date is always a weekday.
Addition to Clami219s answer:
To get the customer(s) that need service on a particular date (as of your request), use
SELECT customer_id
FROM table_name
WHERE
service_start_date <= NOW() AND
WEEKDAY(service_start_date) = WEEKDAY('2014-06-25'); //whatever date you like to fetch
I am retrieving data from a table and show the total SUM of entries. What I want to do is to show the total SUM of entries made on today's date, yesterday and this month. The table is using the unix timestamp format (e.g. 1351771856 for example).
Currently I am using this line to show todays results:
AND comment_date > UNIX_TIMESTAMP() - 24 * 3600";
but that gives me just the entries for the last 24 hours.
Example: So let's say its Friday, 17:00 PM - it gives me the count from Thursday 17:00 PM to Friday 17:00 PM
What I want is to get the results for
Thursday 00:00:00 - 23:59:59 (yesterday in this case)
the results for today (00:00:00 - 23:59:59)
and last week, results that start on Monday, 00:00:00 until "today" (in this case Friday).
I couldn't find a way in the MySQL documentation to achieve this.
This mysql code should work for you:
// Today
AND DATE(from_unixtime(comment_date)) = CURRENT_DATE
// Yesterday
AND DATE(from_unixtime(comment_date)) = DATE_SUB(CURRENT_DATE,INTERVAL 1 DAY)
// This week
AND YEARWEEK(from_unixtime(comment_date), 1) = YEARWEEK(CURRENT_DATE, 1)
// This month
AND YEAR(from_unixtime(comment_date)) = YEAR(CURRENT_DATE)
AND MONTH(from_unixtime(comment_date)) = MONTH(CURRENT_DATE)
Simply use this:
AND comment_date > date_sub(current_date, interval 1 day)
See my answer here, I think it's quite related.
Pull records from orders table for the current week
Consider getting intimate with MySQL's GROUP BY. You will most likely need to know this if you use MySQL.