ORDER BY two colums, but not sorting by time? - mysql

I've posted this question before and found a solution: ORDER BY with two columns in MySQL
It's about sorting events. The priority SHOULD BE that TOP-PREMIUM and PREMIUM events are always on top of each date, no matter what.
The solution was that one column was ENUM and not INT. That's why he sorted wrong.
So now I'm sorting this way: ORDER BY e.date ASC, e.highlight DESC
Now I've another new problem, because all my dates were set to time 00:00:00 the time I asked the question here and thought it's all solved. If I'm setting the time to some hour after 00:00, it will be at the top of all events. Even in top of PREMIUM AND TOP-PREMIUM (highlight) events.
Can I somehow let MySQL ignore the time of the datetime type column date or is there any other way to make this work like I want to?
The problem also is that I can't order by highlight DESC first, because then the whole list won't be sorted by date.

You can use the CAST() function or the DATE_FORMAT function to get the date portion only, so this should work:
ORDER BY CAST(e.date AS DATE), e.highlight DESC
ORDER BY DATE_FORMAT(e.date, '%Y-%m-%d'), e.highlight DESC
Demo: SQL Fiddle
Edit: Updated sql fiddle demo to use 2 columns, you can see that the time portion of the date is not affecting sort order.

You can also use the DATE function, which strips the time component from a DATETIME:
ORDER BY DATE(e.date), e.highlight DESC

Related

How would you query a cumulative sum over x amount of days?

I'm trying to find the cumulative sum of sessions of a link for its first 3 days. I tried this but it doesn't seem to take the date clause into account:
select
date,
link,
sum(sessions) as sessions
from ga
where date <= date+interval 3 day
group by link
But if I manually enter a date, it seems to work. Why is it not seeing date+interval 3 day as a proper date...?
Any help would be greatly appreciated! :)
Date is a column, not a value, you need to provide a specific date entry. Also "between" is a better keyword to use in this situation.
You need to also add date column in GROUP BY clause. Also, avoid using column name as date. It will create confusion.
Try below query :
select date_column,
link,
sum(sessions) as sessions
from ga
where date_column BETWEEN CURDATE()-3 AND CURDATE()
group by link, date_column

MySQL Date Format For Ordering By Date

What is the best way to format a date in a MySQL query?
I am currently running the query below which selects the data but doesn't order the information correctly, I presume this is due to the current format of the 'updatetime' row.
SELECT * FROM updates WHERE udcode='Remote Connection' ORDER BY updatetime DESC LIMIT 20;
The current format is as follows:
31/03/2015 13:41:45
How should this date be formatted in order for the ORDERING to work correctly?
Thanks in advance.
Use:
ORDER BY DATE_FORMAT(updatetime, '%Y-%m-%d %H:%i:%S') DESC
you can change the format of your date in MySQL with DATE_FORMAT
SELECT *, DATE_FORMAT(`updatetime`, '%Y-%m-%d %H:%i:%S') AS mydate FROM updates WHERE udcode='Remote Connection' ORDER BY mydate DESC LIMIT 20;
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
You are doing this correctly in the Query, however if you are getting undesired results then there is a possibility that the data type for your updatetime attribute is not correct.
You should use datetime as the data type so that the query is correctly able to distinguish that the information is a date and knows how to order it properly. For example if you are using a varchar it will think that the first digit reading left to right is the most significant rather than recognising the difference and relationship between days, months, years and time.
If you dont have the correct datatype you would either have to include formatting in a bloated query or you would end up with all the days from 10-19 ordered before the 20-29 and then followed by the days 3-9. In this situation the 30th would be considered to be ordered before the 4th for example.
There will be little relevance by the time you are ordering months or years as the day units will have mixed everything up
You can try following changes
Change date format to
yyyy-mm-dd hh:mm:ss
And edit query to
SELECT * FROM updates WHERE udcode='Remote Connection' ORDER BY `updatetime` DESC LIMIT 0, 20;

mySQL ORDER BY TODAY AND THEN NORMAL SORTING

How can I filter the dates that it would sort first by today then normal?
I have a column with data type datetime, I wanted my results to be sorted showing today's date first and continue normal sorting.
What about
SELECT
...
FROM
...
ORDER BY IF(DATE(datefield=CURRENT_DATE()),0,1), datefield DESC
Edit
Added the DESC to the ORDER BY after the 3rd comment to the OQ

Getting week started date using MySQL

If I have MySQL query like this, summing word frequencies per week:
SELECT
SUM(`city`),
SUM(`officers`),
SUM(`uk`),
SUM(`wednesday`),
DATE_FORMAT(`dateTime`, '%d/%m/%Y')
FROM myTable
WHERE dateTime BETWEEN '2011-09-28 18:00:00' AND '2011-10-29 18:59:00'
GROUP BY WEEK(dateTime)
The results given by MySQL take the first value of column dateTime, in this case 28/09/2011 which happens to be a Saturday.
Is it possible to adjust the query in MySQL to show the date upon which the week commences, even if there is no data available, so that for the above, 2011-09-28 would be replaced with 2011/09/26 instead? That is, the date of the start of the week, being a Monday. Or would it be better to adjust the dates programmatically after the query has run?
The dateTime column is in format 2011/10/02 12:05:00
It is possible to do it in SQL but it would be better to do it in your program code as it would be more efficient and easier. Also, while MySQL accepts your query, it doesn't quite make sense - you have DATE_FORMAT(dateTime, '%d/%m/%Y') in select's field list while you group by WEEK(dateTime). This means that the DB engine has to select random date from current group (week) for each row. Ie consider you have records for 27.09.2011, 28.09.2011 and 29.09.2011 - they all fall onto same week, so in the final resultset only one row is generated for those three records. Now which date out of those three should be picked for the DATE_FORMAT() call? Answer would be somewhat simpler if there is ORDER BY in the query but it still doesn't quite make sense to use fields/expressions in the field list which aren't in GROUP BY or which aren't aggregates. You should really return the week number in the select list (instead of DATE_FORMAT call) and then in your code calculate the start and end dates from it.

Group By Date Timestamp MySQL

I've got the following query on a timestamp(specified by INT(10), which kind of does what I want, but not exactly:
SELECT count(entry_date) as theCount, FROM_UNIXTIME(entry_date, '%Y-%m-%d') AS dd
FROM exp_weblog_titles
WHERE entry_date < UNIX_TIMESTAMP(NOW())
GROUP BY dd
ORDER BY dd DESC
LIMIT 7
That spits out the last 7 dates and the # of entries on those dates that are before today's date. The problem with the query I've created is, it only spits out the date if it actually has an entry on it. When in actuality, I still want that date included even if there are zero entries. Is that possible?
You will need to have a list of dates from somewhere. A lot of people create a calendar table just for this purpose with every date (or every value) that they need so that this calendar table and your data table can be joined to provide this kind of query.
You will find in the long run this is far simpler than any other solution, just takes a moment to set up.
See this article for an example of how to generate such a table.