简体   繁体   中英

DISTINCT datetime in SQL query

I am graphing data from a large database (150K+ rows) and some data points have identical timestamps with different price values.

For example:

time  => 1502050000
price => 1

time  => 1502050000 // identical timestamp
price => 1.1

In the SQL query, I want to ignore duplicate timestamps. I have found that DISTINCT will likely do the job, but I'm stuck with applying this to a datetime field in my database. Here is my working SQL query which pulls in duplicate timestamps.

SELECT time, price FROM price_table WHERE time >= '" . $data_from . "' ORDER BY time ASC

My goal is to get unique timestamps only, and avoid querying duplicates.

You can use aggregation:

SELECT time, MIN(price) as price
FROM price_table
WHERE time >= '" . $data_from . "'
GROUP BY time
ORDER BY time ASC;

Of course, this begs the question of what you want for price when there are duplicates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM