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();
Related
I have four columns ( Driver ID , Start time , end time , minutes spent )
When User logins start time column will be filled and when user logouts end time will be filled and total minutes will be calculated . And everytime when user logins new row will be inserted
I'm trying to display total minutes spent on each particular day
For example
Date (13 - 2 - 2019) - 630 minutes spent
Date ( 12 - 2 - 2019) - 450 minutes spent
etc...
But in my case 12 -2 -2019(4AM) to 13 -2 - 2019(3.59AM) is considered as one day
from backend its coded in such a way that if user has logged from (13-2-2019)(2am) to (13-2-2019)(6AM) .. it will store two rows on database.
like below :-
Driver ID | start time | End time | min spent
3229 | (13-2-2019)(2am)| (13-2-2019)(3.59am)| 120
3229 | (13-2-2019)(4am) (13-2-2019)(6.00am) | 120
From above example the 1st row min spent should be for 12-2-2019 Date . For this i have used BETWEEN query as below .
Below query will output two results : -
$sql = "SELECT * , date(start_time),sum(minutes_spent) FROM
driver_online_session where driver_id='3229' AND
start_time BETWEEN '2019-02-12 03:59:59' AND '2019-02-13 03:59:59'
GROUP by date(start_time)
ORDER BY start_time DESC";
Current Output :-
id 3229 date : 2019-02-13 01:07:20 min spent : 61
id: 3229 date : 2019-02-12 04:00:00 min spent : 1024
My expected output should be
id 3229 date : 2019-02-12 04:00:00 min spent : 1085
Please help me solve this query . Thank you
Have you tried removing the start_time from the group by (and from the select as well, probably) and just grouping by the driver ID instead (or not grouping by anything, since driver ID is already restricted to a single value)?
Since you're also restricting which start times can be returned to a single "day" (as defined by you), it should just return all rows where the start time is within the allowed range, and sum the total of all those rows. And since you already know the "day", you don't really need to output it.
Try this:
SELECT
driver_id, sum(minutes_spent)
FROM
driver_online_session
WHERE
driver_id='3229' AND
start_time BETWEEN '2019-02-12 03:59:59' AND '2019-02-13 03:59:59'
Of course if you need a more generic solution (e.g. where you can show many "days" at once) then this won't work too well - you'd have to find a way to group by your custom time range - but it gives you a starting point.
Found the solution :-
Subtract subtract 4 hours from datetime value and group
$sql = "SELECT driver_id , DATE(start_time - interval 4 hour),
SUM(minutes_spent)
FROM driver_online_session
where driver_id='3229' AND
start_time BETWEEN '2019-02-11 03:59:59' AND '2019-02-14 03:59:59'
GROUP by DATE(start_time - interval 4 hour)
ORDER BY date(start_time) DESC";
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)
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.
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.
I have to columns, both datetimes. One's for the time the appointment starts and the other is for when the appointment ends.
I need a mysql query that'll basically return available time during the day so I can set appointments without them overlapping.
So if I'm open on Monday from 1pm to 6pm and have an appointment from 2pm to 3pm it'll return two results:
DATE 13:00:00 - DATE 14:00:00 and DATE 15:00:00 - DATE 18:00:00
My solution would be to always have 2 datetimes, one being the start time, one being the end time. That would be the 'cleaner' way in my opinion.
The way to get the open appointments is something like this:
SELECT *
FROM appointments
WHERE end_time = "0000-00-00 00:00:00" (or > in stead of = for all appointments)
Or if you leave it empty something like this:
SELECT *
FROM appointments
WHERE end_time IS NULL (or IS NOT NULL for all appointments)