I have a timestamp field added to every of my records with an SQL timestamp, (yes, the format is actually TIMESTAMP). I want to display my records by date and time; can I just ORDER BY in an SQL statement?
SELECT * FROM table ORDER BY timestamp DESC
Assuming its MySQL,
SELECT *
FROM randomTable
ORDER BY timestampfield DESC;
is just perfect.
Yes, I tried it and it is working for me on Oracl SQL Developer
Related
I normally work with MySQL databases, and I am currently encountering some issues on a query towards a SQL Server database.
I'm trying to get the average of a column, grouped by day. This takes anywhere from 20-30 seconds, even if its just returning a few hundred rows.
The table however contains a couple million entries. I'm sure this has got something to do with the indexing properties, but I just can't seem to figure out the correct solution here.
So the query goes like:
select
[unit_id],
avg(weight) AS avg,
max(timestamp) AS dateDay
from
[measurements]
where
timestamp BETWEEN '2017-06-01' AND '2017-10-04'
group by
[unit_id], CAST(timestamp AS DATE)
order by
[unit_id] asc, [dateDay] asc
I have set up a nonclustered index containing the unit_id, weight and timestamp fields.
This is your query:
select unit_id, avg(weight) AS avg, max(timestamp) AS dateDay
from measurements m
where timestamp BETWEEN '2017-06-01' AND '2017-10-04'
group by unit_id, CAST(timestamp AS DATE)
order by unit_id asc, dateDay asc;
Under reasonable assumptions about your data, it is going to have similar performance in either MySQL or SQL Server. Your WHERE is not highly selective. Because of the inequality, SQL Server cannot make use of an index for the GROUP BY.
An index on measurements(timestamp, unit_id, weight) might benefit the query on either database. There might be some fancy ways to get SQL Server to improve the performance. But both it and MySQL will need to take the rows matching the WHERE clause and aggregate them (using a hash-based algorithm in all likelihood in SQL Server and using a filesort in MySQL).
The problem is likely the CAST in the group by. Though you don't say it explicitly, I'm assuming Timestamp is a DateTime value, which is why you CAST to Date in the group by clause. The problem is that the calculated value produced by CAST isn't indexed.
If it's your system, and this query is something done frequently, I'd add a new column of type Date to store just the day, and index that. If you can't, select out the values in the date range you're interested in, with the date casted to Date, into a temp table or CTE, then group by the date.
Or, even try this, just to pull the CAST out of the Group By clause:
select
[unit_id],
avg(weight) AS avg,
dateDay
from (
select [unit_id],
CAST(timestamp as Date) [dateDay],
weight
from [measurements]
where
timestamp BETWEEN '2017-06-01' AND '2017-10-04'
) x
group by
x.[unit_id], x.[dateDay]
order by
x.[unit_id] asc, x.[dateDay] asc
I have a question and I didn't find the solution on stackoverflow. So here is my question:
How can I order a Varchar on datetime (dd/mm/yy hh:mm:ss) priorities in my PHP file?
I have a SQL table with a "registerdate" column. Its type is Varchar (datetime isn't possible due to other table issues). My SQL ORDER BY query is:
SELECT * FROM users ORDER BY registerdate DESC LIMIT 3
This results in:
12-11-16 11:03:54
06-12-16 19:05:22
06-12-16 15:03:30
How I want it:
06-12-16 19:05:22
06-12-16 15:03:30
12-11-16 11:03:54
In my php file I register the time with $datetime=date("d/m/y H:i:s"); and in my table it is a Varchar who looks like 03/12/16 12:19:33
I think I have to CONVERT it in my SQL Query but I don't know how. Who can help me? Many thanks in advance!
-----SERVER/SQL INFO-----
Server: Localhost via UNIX socket
Servertype: MariaDB
Serverversie: 10.0.27-MariaDB-cll-lve - MariaDB Server
Protocolversie: 10
SELECT * FROM users u ORDER BY STR_TO_DATE(u.registerdate, '%d/%m/%Y %H:%i:%s') DESC LIMIT 3
or
SELECT *, STR_TO_DATE(u.registerdate, '%d/%m/%Y %H:%i:%s') as newdate FROM users u ORDER BY newdate DESC LIMIT 3
I think this will work for you too if you are using sql server.
SELECT * FROM users ORDER BY Cast(registerdate AS datetime) DESC
For an Oracle database the syntax would be:
SELECT * FROM users ORDER BY TO_DATE(registerdate, 'DD/MM/YY HH24:MI:SS') DESC
I am storing the date as "14-02-2013" in date column of my table. Now when I get the date using "ORDER BY" then it should display the output as
14-02-2013
15-03-2013
24-05-2013
How to write the query for this. i.e getting order by day and month.
Any suggestion will be helpful.
Use DATE (or DATETIME) type in your column. Otherwise you will have to perform operations on string representation of date, which is not cool and will cost some extra time to perform
Try one of following:
Select * from Table1 order by date(dtcolumn) Asc
Select * from Table1 order by strftime('%d-%m-%Y', dtcolumn)
Try this query
Select * from Table1 Order By mydatecol Asc
Given you store your date as a temporal data type you can use
select * from your_date_table order by date(your_date_column) ASC
If you store it as a string(which you should not do), you can try
select * from your_date_table order by your_date_column ASC
Here is the doc for the sqlite date and time functions
I can easily get a random record with this:
SELECT * FROM MyTable ORDER BY NewId()
I can easily get a record with "today's date" with this:
SELECT * FROM MyTable WHERE MyDate = "2010-24-08" -- db doesn't store times
But how would I combind the two?
Get 1 random record... anything with today's date.
If none are found... get 1 random record from yesterday (today-1).
If none are found... get 1 random record from etc, etc, today-2
... until 1 record is found.
Just make the day date the primary order by condition:
select top(1) *
from Table
order by Date desc, newid();
If you store the dates as full day and time, you need to round them out to the day part only: cast (Date as DATE) in SQL 2008 or cast(floor(cast(Date as FLOAT)) as DATETIME) in pre-2008.
Use the TOP operator:
SELECT TOP 1 *
FROM MyTable
WHERE MyDate = "2010-24-08"
ORDER BY NEWID()
...combined with the ORDER BY NEWID(). Without the ORDER BY, you'd get the first inserted row/record of the records returned by the filteration in most cases typically, but the only way to ensure order is with an ORDER BY clause.
SQL Server 2005+ supports brackets on the TOP value, so you can use a variable in the brackets without needing to use dynamic SQL.
Does this give you what you want?
SELECT TOP 1 *
FROM MyTable
ORDER BY MyDate desc, NewId()
This assumes there are no dates later than today.
I want to show records from a table having a date column from sqldatabase in dates order. How should I?
SELECT
*
FROM yourTable
ORDER BY yourDateColumn
you can use order by
select * from my_table t order by t.date_column
where date_column is a column name in your table.
SELECT * FROM `table_name` ORDER BY `dates_column`
optionally you can add DESC to reverse the order (newest to oldest):
SELECT * FROM `table_name` ORDER BY `dates_column` DESC
If I understand your question correctly, you want to use an ORDER BY clause on the column containing the dates.
The database engine should handle the proper sorting for a date or timestamp column using an ORDER BY clause. The only exception might be if your column is of type VARCHAR and holds a date of the form "mm/dd/yyyy". Then you've got a bit more work to do.