I have a select statement that selects the time, and order by the time asc, but it seems to be ignoring the A.M and P.M part. How would I fix that?
SELECT DATE_FORMAT(max(time),'%h:%i:%s %p') AS mytimestr FROM currentChat WHERE date = '$date' ORDER BY mytimestr ASC
Result:
1:00 A.M
2:00 P.M
3.00 A.M
4.00 P.M
Need Result:
1:00 A.M
3.00 A.M
2:00 P.M
4.00 P.M
If your time column is a MySQL TIME datatype, as I suspect it is from the way you call DATE_FORMAT, then you don't need to ORDER BY your mytimestr expression. You can simply order by the time field itself to get the records in chronological order.
SELECT DATE_FORMAT(max(time),'%h:%i:%s %p') AS mytimestr
FROM currentChat
WHERE date = '$date'
ORDER BY max(time) ASC
I'm not sure whether you intended to have MAX(...) without a GROUP BY - this will only return one row making the ORDER BY a little bit redundant but perhaps you only gave a portion of your query.
Related
I have a table, that is updated every minute and I need to calculate the average value of each hour, for the values of the last 30 days.
Timestamp | SB1_AC_GES_DIFF
2020-07-14 15:13:04 30
2020-07-14 15:12:07 27
... ...
I want to save the results in a second table named avgTable like this
Timestamp | AVG_SB1
15:00 29
16:00 32
... ...
It would be perfect if the table could update itself once a day, maybe when it's 12 o'clock and the the date part for the day changes.
You can try:
INSERT INTO avg_table
SELECT Date_format(Timestamp, "%h:00:00") AS HourlyTimeStamp,
Avg(sb1_ac_ges_diff) AS "AVG_SB1"
FROM table
WHERE Timestamp between DATEADD(DAY, -30, GETDATE()) AND GETDATE()
GROUP BY 1
Assuming that you want the average rolling average, agnostic of the day.
You can just use the hour() function:
select hour(timestamp) as hh, avg(sb1_ac_ges_diff)
from t
group by hh;
You can convert this to a string or time if you want, but that does not seem useful to me.
If you actually want the hour for each day, then:
select date(timestamp) as dd, hour(timestamp) as hh, avg(sb1_ac_ges_diff)
from t
group by dd, hh;
How do I query between two time range using MySQL?
it is similar to this question provided in the above link but the match_time was divided into two columns, i.e. match_start_time and match_end_time,
match_start_time <= CAST('10:00:00' AS time) AND match_end_time >= (CAST('12:00:00' AS time))
This was the query through which i tried but was not getting the correct result.
example
consider match start and end time being:-
01:30 - 03:30, 05:00 - 06:30, 03:00 - 21:30, 14:00 - 09:00
then if i pass 00:00 - 10:00 as min and max, then i get
01:30 - 03:30, 05:00 - 06:30, 14:00 - 09:00
but not sure whether 14:00 - 09:00 should be included.
Also if they pass 18:00 - 09:00
Then how to get the result if user provided min time is greater than max time
Sorry for bad English, please help
The question is bit ambiguous. You can convert the times to datetime and determine into which date the times belong to:
select time_range, cast(time_to_match as time)
from (
select
concat(m.match_start_time,'-',m.match_end_time) as 'time_range',
addtime(cast(concat('2020-01-', if(m.match_start_time<=m.match_end_time or m.match_start_time<t.match_time, '01', '02')) as datetime),t.match_time) as 'time_to_match',
addtime(cast('2020-01-01' as datetime), m.match_start_time) as 'start_time',
addtime(cast(concat('2020-01-', if(m.match_start_time<=m.match_end_time, '01', '02')) as datetime), m.match_end_time) as 'end_time'
from times t
join match_times m on 1=1
) as q
where time_to_match between start_time and end_time
order by 1,2;
See db-fiddle.
The SQL I'm using on MySQL database is
SELECT
CONCAT(YEAR(EVE_DATE),'-',MONTH(EVE_DATE),'-',DAYNAME(EVE_DATE)) AS WEEKDAY_DATE,
SUM(EVE_OCCUR)
FROM
TABLE
WHERE
EVE_DATE BETWEEN '2015-01-01' AND '2015-10-31'
GROUP BY
WEEKDAY_DATE
ORDER BY WEEKDAY_DATE
The output of the weekname it generates is in the format "YYYY-MON-DAY". Currently, the output is not ordered. I would like to order it as below
2015-01-Sun
2015-01-Mon
2015-01-Tue
2015-01-Wed
2015-01-Thu
2015-01-Fri
2015-01-Sat
2025-02-Sun
2015-02-Mon
2015-02-Tue
2015-02-Wed
2015-02-Thu
2015-02-Fri
2015-02-Sat
and so on
Could someone please help me?
You can order by EVE_DATE:
SELECT
CONCAT(YEAR(EVE_DATE),'-',MONTH(EVE_DATE),'-',DAYNAME(EVE_DATE)) AS WEEKDAY_DATE,
SUM(EVE_OCCUR)
FROM TABLE
WHERE EVE_DATE BETWEEN '2015-01-01' AND '2015-10-31'
GROUP BY WEEKDAY_DATE
ORDER BY EVE_DATE
SqlFiddleDemo
EDIT:
Example what I wanted to communicate in comment:
2015-01-01 - Thursday -- 4th
2015-01-02 - Friday -- 5th
2015-01-03 - Saturday -- 6th
2015-01-04 - Sunday -- 7th
2015-01-05 - Monay -- should it be the first
2015-01-06 - Tuesday -- 2nd
2015-01-07 - Wednesday -- 3rd
2015-01-08 - Thurday
2015-01-09 - Friday
2015-01-10 - Saturday
2015-01-11 - Sunday
2015-01-12 - Monday -- 8th
When you sort by date you sort by actual date and not its textual representation.
Now you want to start from Monady but Monday does not exists in this year/month as starting point. Do you want to shuffle Monady? What you propose is nonsense for me.
Why are you using that concat() statement when MySQL has date_format()?
SELECT DATE_FORMAT(eve_date, '%Y-%m-%a') as weekday_date,
SUM(EVE_OCCUR)
FROM TABLE
WHERE eve_date >= '2015-01-01' AND eve_date < '2015-02-01'
GROUP BY WEEKDAY_DATE
ORDER BY MIN(eve_date);
For the ORDER BY, I would recommend using an aggregation function, rather than ORDER BY eve_date. This is consistent with how standard SQL works.
I also changed the date comparisons to use >= and <. BETWEEN is a dangerous habit with dates, because it works differently when there is a time component. The above method works equally well for dates and date/times.
You can use EVE_DATE column to sort the output in Query
SELECT
CONCAT(YEAR(EVE_DATE),'-',MONTH(EVE_DATE),'-',DAYNAME(EVE_DATE)) AS WEEKDAY_DATE,
SUM(EVE_OCCUR)
FROM
TABLE
WHERE
EVE_DATE BETWEEN '2015-01-01' AND '2015-10-31'
GROUP BY
WEEKDAY_DATE
ORDER BY YEAR(EVE_DATE),MONTH(EVE_DATE) ,FIELD(DAYNAME(EVE_DATE), 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');
I have a range of dates like
2015-05-31 17:36:36.000000
to
2015-12-20 22:20:00.000000.
well, i want fetch rows which are between 17 and 19 of each day in that period of time. of course it is important that include minutes too.
i mean, in every single day, if a row has a time which is between 17 to 20, i need that.
What query should i use?
i used
SELECT *
FROM (`orktape`)
WHERE STR_TO_DATE(timestamp, '%Y-%m-%d %H:%i:%s') >= '2015-05-31 17:36:36.000000'
AND STR_TO_DATE(timestamp, '%Y-%m-%d %H:%i:%s') < '2015-12-20 22:20:00.000000'
ORDER BY `timestamp` desc
but it result all the rows which are between 2015-05-31 17:36:36.000000 and 2015-12-20 22:20:00.000000 which include some rows like 2015-12-20 02:20:00.000000 which is not the correct result.
Thanks.
You can select in a range of hours by...
SELECT * FROM orktape WHERE HOUR(mytimestamp) >= 17 AND HOUR(mytimestamp) < 19
Not sure what your timestamp column is called, I would not call it timestamp, that could be confusing.
I'm trying to pull all information inside a given date range from my database using something like this query:
SELECT transactionDate, SUM(transactionTotal)
FROM transaction
WHERE transactionDate BETWEEN '2014-06-01' AND '2014-08-11'
AND transactionType = 'end'
GROUP BY DAYOFMONTH(transactionDate)
ORDER BY transactionDate ASC
But somehow I only get the data for 1 month
2014-06-01 20:38:05 4811500.00
2014-06-02 20:42:59 5924950.00
2014-06-03 20:44:38 3811500.00
2014-06-04 11:45:13 4472000.00
2014-06-05 15:34:53 7922000.00
2014-06-06 17:45:28 5027000.00
2014-06-07 11:25:38 4378000.00
2014-06-08 07:59:04 4250000.00
2014-06-09 08:41:49 4766500.00
2014-06-10 01:23:35 4071000.00
2014-06-11 01:01:30 1459000.00
2014-06-12 15:05:08 2960000.00
2014-06-13 00:47:09 1160000.00
2014-06-14 16:52:20 4208000.00
2014-06-16 00:05:18 3947500.00
2014-06-17 00:18:39 4926000.00
2014-06-18 00:33:38 4244500.00
2014-06-19 00:43:39 4045000.00
2014-06-20 22:47:54 2649500.00
2014-06-21 23:06:04 4030000.00
2014-06-22 23:19:22 945500.00
2014-06-23 23:29:27 3015000.00
2014-06-24 23:35:56 2420000.00
2014-06-25 00:02:03 3920000.00
2014-06-26 00:50:33 4841000.00
2014-06-27 10:39:14 4095000.00
2014-06-28 07:43:06 5605500.00
2014-06-29 11:48:24 1939000.00
2014-06-30 10:49:50 3620000.00
As you can see, I get the results from 2014-06-01 to 2014-06-01 even when all other dates also have data to display.
Hope you can help me, thanks!
You're misusing GROUP BY and the pernicious MySQL GROUP BY extension is biting you. You're grouping your data into 31 buckets with your GROUP BY DAYOFMONTH() , so there's no way you'll get more than 31 rows in your result set. You're then (misusing the extension) displaying some arbitrarily selected transaction_date value from each of your 31 groupings. It happens to be a June date, and this has tricked you into thinking you've only selected June records. You've actually selected all the dates in your range, but mistakenly grouped July and August records with your June records. Ouch.
Second, using BETWEEN to select date ranges from a DATETIME column isn't quite right, because it gets the end of the range wrong. BETWEEN '2014-06-01' AND '2014-08-11' gets the items dated from the first of June up until the very first moment of August 11th. So something happening at noon on August 11th won't get selected.
Here is the query you need to do this job correctly.
SELECT DATE(transactionDate),
SUM(transactionTotal)
FROM transaction
WHERE transactionDate >= '2014-06-01'
AND transactionDate < '2014-08-11' + INTERVAL 1 DAY
AND transactionType = 'end'
GROUP BY DATE(transactionDate)
ORDER BY DATE(transactionDate) ASC
What's going on here? First, we're grouping by just the date of each transaction, using DATE(), and we're including that grouped value in our result set with SELECT DATE().
Second, the date range selection takes everything on or after midnight on June first, up until the moment right before midnight on August 12th. That's the point of transactionDate < '2014-08-11' + INTERVAL 1 DAY. It takes the whole day's worth of items for August 11.
There's a writeup of this corner of SQL tech here: http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/
Use the following sql query to get desired results
SELECT transactionDate, SUM(transactionTotal) FROM transaction WHERE transactionDate BETWEEN '2014-06-01' AND '2014-08-11' AND transactionType = 'end' GROUP BY CAST(transactionDate AS DATE) ORDER BY transactionDate ASC