greatest-n-per-group

Return row with the max value of one column per group [duplicate]

和自甴很熟 提交于 2019-11-27 02:06:25
This question already has an answer here: Fetch the row which has the Max value for a column 34 answers Oracle SQL query: Retrieve latest values per group based on time [duplicate] 2 answers Get top results for each group (in Oracle) 5 answers GROUP BY with MAX(DATE) [duplicate] 6 answers I am having a hard time doing this without searching the same table at least twice in order to grab the max row, and then grab the value for that row. The table in question is quite big so this is unacceptable. Here is what my table might look like: SCORES ID ROUND SCORE 1 1 3 1 2 6 1 3 2 2 1 10 2 2 12 3 1 6

how to sort order of LEFT JOIN in SQL query?

守給你的承諾、 提交于 2019-11-27 00:51:20
问题 OK I tried googling for an answer like crazy, but I couldn't resolve this, so I hope someone will be able to help. Let's say I have a table of users, very simple table: id | userName 3 Michael 4 Mike 5 George and I have another table of their cars and their prices. id | belongsToUser | carPrice 1 4 5000 2 4 6000 3 4 8000 Now what I need to do is something like this (feel free to rewrite): SELECT `userName`, `carPrice` FROM `users` LEFT JOIN `cars` ON cars.belongsToUser=users.id WHERE `id`='4'

MYSQL how to select data where a field has a min value

ⅰ亾dé卋堺 提交于 2019-11-27 00:43:37
问题 Please I want to select data from a table, where a specific field has the min value, I've tried this : SELECT * FROM pieces where min(price) I'm not good with MySQL, please any help ? Thanks 回答1: this will give you result that has the minimum price on all records. SELECT * FROM pieces WHERE price = ( SELECT MIN(price) FROM pieces ) SQLFiddle Demo 回答2: This is how I would do it (assuming I understand the question) SELECT * FROM pieces ORDER BY price ASC LIMIT 1 If you are trying to select

GROUP BY having MAX date

时光毁灭记忆、已成空白 提交于 2019-11-27 00:26:23
问题 I have problem when executing this code: SELECT * FROM tblpm n WHERE date_updated=(SELECT MAX(date_updated) FROM tblpm GROUP BY control_number HAVING control_number=n.control_number) Basically, I want to return the most recent date for each control number. The query above returns correct output but it takes 37secs. before the output was shown. Is there any other sql clause or command that can execute faster than the query above? Thanks in advance. 回答1: Putting the subquery in the WHERE clause

Select the max row per group - pandas performance issue

我只是一个虾纸丫 提交于 2019-11-26 23:10:50
问题 I'm selecting one max row per group and I'm using groupby / agg to return index values and select the rows using loc . For example, to group by "Id" and then select the row with the highest "delta" value: selected_idx = df.groupby("Id").apply(lambda df: df.delta.argmax()) selected_rows = df.loc[selected_idx, :] However, it's so slow this way. Actually, my i7/16G RAM laptop hangs when I'm using this query on 13 million rows. I have two questions for experts: How can I make this query run fast

Get the latest date from grouped MySQL data

a 夏天 提交于 2019-11-26 22:20:24
I have the following data in my database: |NO | model | date | +---+-------+----------+ |1 | bee |2011-12-01| |2 | bee |2011-12-05| |3 | bee |2011-12-12| |4 | tar |2011-12-13| I want to get the latest date of each model group: | model | date | +-------+----------+ | bee |2011-12-12| | tar |2011-12-13| I tried: SELECT model, date FROM doc WHERE date ........????? //what is the next? GROUP BY model Are you looking for the max date for each model? SELECT model, max(date) FROM doc GROUP BY model If you're looking for all models matching the max date of the entire table... SELECT model, date FROM

Using ORDER BY and GROUP BY together

安稳与你 提交于 2019-11-26 21:54:15
My table looks like this (and I'm using MySQL): m_id | v_id | timestamp ------------------------ 6 | 1 | 1333635317 34 | 1 | 1333635323 34 | 1 | 1333635336 6 | 1 | 1333635343 6 | 1 | 1333635349 My target is to take each m_id one time, and order by the highest timestamp. The result should be: m_id | v_id | timestamp ------------------------ 6 | 1 | 1333635343 34 | 1 | 1333635336 And i wrote this query: SELECT * FROM table GROUP BY m_id ORDER BY timestamp DESC But, the results are: m_id | v_id | timestamp ------------------------ 34 | 1 | 1333635323 6 | 1 | 1333635317 I think it causes because

PostgreSQL Selecting Most Recent Entry for a Given ID

心不动则不痛 提交于 2019-11-26 21:42:58
问题 Table Essentially looks like: Serial-ID, ID, Date, Data, Data, Data, etc. There can be Multiple Rows for the Same ID. I'd like to create a view of this table to be used in Reports that only shows the most recent entry for each ID. It should show all of the columns. Can someone help me with the SQL select? thanks. 回答1: There's about 5 different ways to do this, but here's one: SELECT * FROM yourTable AS T1 WHERE NOT EXISTS( SELECT * FROM yourTable AS T2 WHERE T2.ID = T1.ID AND T2.Date > T1

How to select id with max date group by category in PostgreSQL?

一曲冷凌霜 提交于 2019-11-26 21:29:47
For an example, I would like to select id with max date group by category, the result is: 7, 2, 6 id category date 1 a 2013-01-01 2 b 2013-01-03 3 c 2013-01-02 4 a 2013-01-02 5 b 2013-01-02 6 c 2013-01-03 7 a 2013-01-03 8 b 2013-01-01 9 c 2013-01-01 May I know how to do this in PostgreSQL? This is a perfect use-case for DISTINCT ON (Postgres specific extension of standard DISTINCT ): SELECT DISTINCT ON (category) id -- , category, date -- add any other column (expression) from the same row FROM tbl ORDER BY category, "date" DESC; Careful with descending sort order. If the column can be NULL,

SELECT query return 1 row from each group

给你一囗甜甜゛ 提交于 2019-11-26 20:44:02
This is a product table and have few million of records. I want to list record as below: Normally I use: SELECT id, product_name, store_id FROM product GROUP BY store_id ORDER BY id. Currently having SQL performance issue. I need SQL query to output result like this. John Woo There are many alternatives to solves this, one which I recommend is to have joined a subquery which separately gets the latest ID ( assuming that the column is AUTO_INCREMENT ed ) for each store_ID . SELECT a.* FROM tableName a INNER JOIN ( SELECT store_ID, MAX(ID) max_ID FROM tableName GROUP BY store_ID ) b ON a.store