greatest-n-per-group

PostgreSQL: top n entries per item in same table

安稳与你 提交于 2019-11-28 01:15:42
问题 | uId | title | amount | makers | widgets | 1 richard 998 xcorp sprocket 2 swiss 995 ycorp framitz 3 ricky 90 zcorp flobber 4 ricky2 798 xcorp framitz 1 lilrick 390 xcorp sprocket 1 brie 200 mcorp gullywok 1 richard 190 rcorp flumitz 1 brie 490 bcorp sprocket etc... I am trying to retrieve only 3 records per makers , the top 3 amounts and the widgets they produced Here's is what I have: SELECT amount, makers FROM (SELECT amount, makers, (SELECT count(*) FROM entry as t2 WHERE t2.amount = t1

Select second most minimum value in Oracle

喜夏-厌秋 提交于 2019-11-28 00:34:46
问题 I need to write a query that selects a minimum value and it's second most minimum value from a list of integers. Grabbing the smallest value is obvious: select min(value) from table; But the second smallest is not so obvious. For the record, this list of integers is not sequential -- the min can be 1000, and the second most min can be 10000. 回答1: Use an analytic function SELECT value FROM (SELECT value, dense_rank() over (order by value asc) rnk FROM table) WHERE rnk = 2 The analytic

PostgreSQL Selecting Most Recent Entry for a Given ID

穿精又带淫゛_ 提交于 2019-11-28 00:28:28
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. 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.Date ) And here's another: SELECT T1.* FROM yourTable AS T1 LEFT JOIN yourTable AS T2 ON ( T2.ID = T1.ID AND T2

How can a do a “greatest-n-per-group” query in django?

*爱你&永不变心* 提交于 2019-11-28 00:19:02
问题 (This is the django version of the thread at SQL join: selecting the last records in a one-to-many relationship) Suppose I have a table of customers and a table of purchases. Each purchase belongs to one customer. I want to get a list of all customers along with their last purchase. Can it be done without raw SQL and without multiple database queries? 回答1: You can take a look at similar discussion: Django Query That Get Most Recent Objects From Different Categories 回答2: You can't do this in

Select the max row per group - pandas performance issue

心不动则不痛 提交于 2019-11-27 23:17:01
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 in pandas? What am I doing wrong? Why is this operation so expensive? [Update] Thank you so much for

Selecting rows ordered by some column and distinct on another

南笙酒味 提交于 2019-11-27 20:09:58
Related to - PostgreSQL DISTINCT ON with different ORDER BY I have table purchases (product_id, purchased_at, address_id) Sample data: | id | product_id | purchased_at | address_id | | 1 | 2 | 20 Mar 2012 21:01 | 1 | | 2 | 2 | 20 Mar 2012 21:33 | 1 | | 3 | 2 | 20 Mar 2012 21:39 | 2 | | 4 | 2 | 20 Mar 2012 21:48 | 2 | The result I expect is the most recent purchased product (full row) for each address_id and that result must be sorted in descendant order by the purchased_at field: | id | product_id | purchased_at | address_id | | 4 | 2 | 20 Mar 2012 21:48 | 2 | | 2 | 2 | 20 Mar 2012 21:33 | 1 |

How do I join the most recent row in one table to another table?

旧巷老猫 提交于 2019-11-27 19:45:56
问题 I have data that looks like this: entities id name 1 Apple 2 Orange 3 Banana Periodically, a process will run and give a score to each entity. The process generates the data and adds it to a scores table like so: scores id entity_id score date_added 1 1 10 1/2/09 2 2 10 1/2/09 3 1 15 1/3/09 4 2 10 1/03/09 5 1 15 1/4/09 6 2 15 1/4/09 7 3 22 1/4/09 I want to be able to select all of the entities along with the most recent recorded score for each resulting in some data like this: entities id

How can I select rows with most recent timestamp for each key value?

烈酒焚心 提交于 2019-11-27 17:23:03
I have a table of sensor data. Each row has a sensor id, a timestamp, and other fields. I want to select a single row with latest timestamp for each sensor, including some of the other fields. I thought that the solution would be to group by sensor id and then order by max(timestamp) like so: SELECT sensorID,timestamp,sensorField1,sensorField2 FROM sensorTable GROUP BY sensorID ORDER BY max(timestamp); This gives me an error saying that "sensorField1 must appear in the group by clause or be used in an aggregate." What is the correct way to approach this problem? For the sake of completeness,

SQL Query: Return Max value record of a Group

亡梦爱人 提交于 2019-11-27 16:30:00
I have a sample table with similar structure & data as shown below: +------+---------+-------------+------------+ | S_ID | S_NAME | SUBJECT | MARK_VALUE | +------+---------+-------------+------------+ | 1 | Stud | SUB_1 | 50 | | 2 | Stud | SUB_2 | 60 | | 3 | Stud | SUB_3 | 70 | | 4 | Stud_1 | SUB_1 | 40 | | 5 | Stud_1 | SUB_2 | 50 | | 6 | Stud_2 | SUB_2 | 40 | +------+---------+-------------+------------+ Table has consolidated mark of each student in all subjects each that student has appeared. Please help me, to write a query to extract MAXIMUM mark obtained by each student ( irrespective of

How to get all the fields of a row using the SQL MAX function?

一曲冷凌霜 提交于 2019-11-27 15:17:10
问题 Consider this table (from http://www.tizag.com/mysqlTutorial/mysqlmax.php): Id name type price 123451 Park's Great Hits Music 19.99 123452 Silly Puddy Toy 3.99 123453 Playstation Toy 89.95 123454 Men's T-Shirt Clothing 32.50 123455 Blouse Clothing 34.97 123456 Electronica 2002 Music 3.99 123457 Country Tunes Music 21.55 123458 Watermelon Food 8.73 This SQL query returns the most expensive item from each type: SELECT type, MAX(price) FROM products GROUP BY type Clothing $34.97 Food $8.73 Music