$today = mktime(0,0,0);
$yesterday = $today - 86400;
$this->view->numberyesterday=$db->fetchAll("SELECT * FROM `transactions` WHERE `siteid`=".$sid." AND `timestamp` > ".$db->quote($yesterday,'integer')." AND timestamp < ".$db->quote($today,'integer'));
I have used the above code to display all the records of yesterday. Now I need to plot the graph for each hour. How can I retrieve the records in every hour from the above query?( like, records within 12-1 AM, 1-2 AM....23-24PM etc)
Thanks!
Edit:
I wish I could store the records of each hour in each variable. So that there will be 24 variable for 24 hours. So it is easy to draw the graph.
You did not specify how the output would look like, but it seems like this construct would help you:
select Hour(timestamp) transhour, count(*) transcount
from transactions
where yourfilters = yourvalues
Group by Hour(timestamp)
The output will look like this:
transhour | transcount
======================
0 | 2
1 | 23
2 | 45
3 | 23
... | ...
22 | 34
23 | 3
Warning: if no transactions were performed in a given hour, the output will omit this hour (and not bring it back with count zero)
Related
I have a query that gets executed based on the date range the user chooses. For example: 12-12-2019 to 1-13-2020.
// Retrieve count of attendance, no shows and cancellations per user selected category and sort by week number
$q = "SELECT YEARWEEK(`start_time`, 0) AS weekno,
SUM(`is_no_show` = 0 AND `is_cancelled` = 0) as attended,
SUM(`is_no_show` = 1) AS no_shows,
SUM(`is_cancelled` = 1)AS cancelled
FROM `myTable`
WHERE (`start_time` > :start_date AND `start_time` < :end_date)
AND category LIKE :cohort_type
GROUP BY weekno";
My issue is that this query stops pulling in data after 12-23-2019. It seems to stop at the last week of the year and not go into 2020 as week 1. How do I account for this? Any suggestions or tips is greatly appreciated!
Thank you.
General DB Structure:
+------------+-----------+-----------+
| start_time | no_shows | cancelled |
+------------+-----------+-----------+
| 2019-12-20 | 1 | 0 |
| 2019-12-21 | 0 | 0 |
| 2019-12-22 | 0 | 1 |
GOAL: I want to SUM the data on a weekly basis
EDIT: YEARWEEK() skips the first week of 2020 and goes straight to week 2.
Your query is missing a GROUP BY criteria on the year; as of now, it will mix weeks belonging to different years, which I assume is not what you want. I would suggest using YEARWEEK(), which takes the year in account.
Also, your ilter on the date seems akward, as it is mixing parameters and string concatenation; you can use half-open intervals instead (and proper parameter bindings).
Consider:
SELECT
YEARWEEK(`start_time`) AS weekno,
SUM(`is_no_show` = 0 AND `is_cancelled` = 0) attended,
SUM(`is_no_show` = 1) no_shows,
SUM(`is_cancelled` = 1) cancelled
FROM `attendee_categories_appts_joined`
WHERE
`start_time` > :start_date
AND `start_time` < :end_date
AND category LIKE :cohort_type
GROUP BY weekno
I have a booking system for hotel rooms, sometimes there are reservations that start in one month and finish in another month. For example 22 April 2019 to 07 May 2019.
I need to be able to see how many days the room was booked in April.
Can't figure out how to reduce the overlapping days in the next month.
So if I have those bookings:
07/Apr/2019 to 12/Apr/2019
22/Apr/2019 to 07/May/2019
29/May/2019 to 03/June/2019
It should give me:
April 2019: 13 Days
May 2019: 10 Days
June 2019: 3 Days
I tried using DATEDIFF and SUM but it adds MAY Dates also.
$sql = "SELECT
id, SUM(DATEDIFF(booking_to, transaction_date)) + 1 AS occup,
COALESCE(COUNT(id), 0) AS `transactions`,
COALESCE(SUM(revenue), 0) AS `revenue`
FROM transactions
WHERE property_id = $pvid
AND MONTH(transaction_date) = $tranmonth
AND YEAR(transaction_date) = $tranyear
group by property_id";
Currently it is giving me 20 Days which is incorrect.
Is it possible to the amount of days booked only for April?
what you probably need is the last day of the month and diff it after
SELECT LAST_DAY(NOW());
edit:
select now() as now, timestampdiff(day, now(), last_day(now())) as diff;
+---------------------+------+
| now | diff |
+---------------------+------+
| 2019-05-04 05:48:43 | 26 |
+---------------------+------+
1 row in set (0.00 sec)
I need to calculate the avg days between date of sales:
My DB is like this:
id | customer | creation_date | payment_date
1 | 234 | 2017/07/6 | 2017/07/8
34 | 234 | 2017/08/4 | 2017/08/10
53 | 234 | 2017/09/15 | 2017/09/17
67 | 234 | 2017/10/1 | 2017/07/6
So I need to calculate de difference of days (creation_date) between Order 1 and Order 34, Order 34 and Order 53, Order 53 and Order 67, etc...
and calculate an AVG of days depending the number of results.
So I know how to calculate the difference of days between 2 dates using this small script:
$seconds=strtotime($date1) - strtotime($date2);
$difference=intval($seconds/60/60/24);
$positive = $difference * -1;
but I donĀ“t know how to take the date of the las result and compare it with the next result.
Please someone who can help me with this enigma. Thanks!
I could be misunderstanding what you are looking for, but I would think something like this should work
(TO_DAYS(MAX(creation_date))-TO_DAYS(MIN(creation_date))) / (COUNT(1)-1)
This will get you the total days between the first and last; and divide by the number of "spaces" between orders.
Edit: ....and if you wanted to treat orders on the same date as a single order, you can just change COUNT(1) to COUNT(DISTINCT creation_date).
...all this assumes the db designer was sane and actually used DATE data types for date values.
To summarize, the average of the span sizes should be the same as the total span divided by the number of spans.
You can keep track of the previous result using a variable outside of the loop to get your MySQL table and then run the loop through the rows of the table:
$last_positive = 0;
while ($row = $result->fetch_assoc()){
$date1 = $row['creation_date'];
$date2 = $row['payment_date'];
$seconds=strtotime($date1) - strtotime($date2);
$difference=intval($seconds/60/60/24);
$positive = abs($difference);
//DO SOME COMPARISON HERE
echo($last_positive >= $positive);
$last_positive = $positive;
}
I'd also suggest using abs to get the absolute value instead of multiplying by -1.
SOLVED WITH THIS:
SELECT DATEDIFF(MAX(creation_date), MIN(creation_date)) / (COUNT(creation_date) - 1) AS SaleAverage FROM table WHERE customer = '$customer'
I am writing a customer loyalty software for a club that opens from 10am to 6am everyday. The data is store in MYSQL and I'd like to count the customer's total visits for the month.
I am using count(distinct(date)) but if the player came at 5pm and stayed till 3am with 2 transactions at 10pm and 2am. It will be counted as 2 visits instead of 1.
I have a transaction table with the columns listed below:
ps: anything in the brackets () is not real data. I get about 2000 transactions a day. I am also able to change the table structure
Transaction_ID | Date(not Date/Time) | Customer_ID | Item | price | timestamp
1 | 11-06-2015 (6pm) | Jane | drink| 2.00 | 156165166
2 | 09-06-2015 (2pm) | Jane | drink| 2.00 | 1433858493
3 | 10-06-2015 (3am) | Jane | drink| 2.00 | 1433906073
4 | 06-06-2015 (6pm) | Jane | drink| 2.00 | 156165166
Current code returns {4, Jane}. The answer I'm looking for is {3,Jane}. Transaction {2,3} should be considered as one visit
SELECT count(distinct(Date)) as visit, Customer_ID
FROM transaction
GROUP BY Customer_ID
WHERE timestamp BETWEEN $timestamp1 AND $timestamp2
$timestamp1 = strtotime("first day of february +10am");
$timestamp2 = strtotime("first day of march +6am");
How do you suggest to accurately count the total visits below? I am able to change the table structure from Date to Date/time.
The easiest answer with least changes to my codes.
SELECT count(DISTINCT(DATE(DATE_SUB(from_unixtime(timestamp),INTERVAL 6 HOUR))) as visit, Customer_ID
FROM transaction
GROUP BY Customer_ID
WHERE timestamp BETWEEN $timestamp1 AND $timestamp2
The easiest way is to shift your datetime (date,timestamp?) field back for 6 hours in a SQL statement and then you will get an interval in one day from 4AM to 12PM:
DISTINCT(DATE(DATE_SUB(dt,INTERVAL 6 HOUR)))
SQLFiddle demo
Here is the code you need:
SELECT
Customer_ID 'Customer ID'
, COUNT(DISTINCT visit) as 'Visits per month'
, MONTH(visit) 'Month'
, YEAR(visit) 'Year'
FROM
(SELECT
*
, CASE
WHEN (t_timestamp > Date_StartDate AND t_timestamp < Date_EndDate)
THEN d_date
WHEN (t_timestamp < Date_StartDate)
THEN date_add(d_date, INTERVAL -1 DAY)
END 'visit'
FROM
(SELECT *
, DATE_ADD(CAST(d_date AS DATETIME), INTERVAL 10 HOUR) Date_StartDate
, DATE_ADD(DATE_ADD(cast(d_date AS DATETIME), INTERVAL 6 HOUR), INTERVAL 1 DAY) Date_EndDate
FROM transactions) Results
) Results
GROUP BY customer_id, month(visit), year(visit)
Also, here is a SQLFiddle with the results of the code.
I haven't used the exact format for your Customer_ID (I've used INTEGER instead of VARCHAR) and didn't use the exact dates you used in your example, but obviously it should work for anything.
Consider adjusting the name of the columns used in my query to the appropriate column names and you should be fine.
I have in my database:
id | text | date
1 | sdsd | 2012-01-23 08:11:00
2 | asd | 2012-01-23 08:24:00
3 | dfdf | 2012-01-23 08:34:00
4 | fdf | 2012-01-23 08:41:00
5 | xcvx | 2012-01-23 08:48:00
etc
on my server is cron with 10 minutes intervals,
for example:
08:03:00
08:13:00
08:23:00
08:33:00
08:43:00
08:53:00
how is the best method for get this values from databases with SQL for PHP?
This query will return all rows that have date > last 10 minutes:
SELECT *
FROM `table`
WHERE `date` > CURRENT_TIMESTAMP - INTERVAL 10 MINUTE
It is assumed that your table does not contain any future dates.
Do a SQL query which looks for a time in the date field that is plus or minus 4 minutes of the current time according to PHP.
In your cron script you could do something like this:
$res = mysql_query('select * from table where `date` < now()');
Then you simply do the thing you want to do with those rows.