greatest-n-per-group

MySQL LIMIT in a Correllated Subquery

a 夏天 提交于 2019-12-02 07:11:40
I have a correlated subquery that will return a list of quantities, but I need the highest quantity, and only the highest. So I tried to introduce an order by and a LIMIT of 1 to achieve this, but MySQL throws an error stating it doesn't yet support limits in subqueries. Any thoughts on how to work around this? SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost, FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price FROM `Product` JOIN `ProductOption` ON Product.idProduct = ProductOption.Product_idProduct JOIN `ProductOptionTier` AS a ON a

MySQL LIMIT in a Correllated Subquery

て烟熏妆下的殇ゞ 提交于 2019-12-02 06:48:36
问题 I have a correlated subquery that will return a list of quantities, but I need the highest quantity, and only the highest. So I tried to introduce an order by and a LIMIT of 1 to achieve this, but MySQL throws an error stating it doesn't yet support limits in subqueries. Any thoughts on how to work around this? SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost, FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price FROM `Product` JOIN

Get the latest date for each record

 ̄綄美尐妖づ 提交于 2019-12-02 06:19:25
I have a History table (like a log) that records changes to parts: TransactionID Part ID Description Last Updated 1 1 Fixed O-ring 2006-03-14 20:00:04.700 2 2 Replaced coil 2009-01-02 20:00:04.700 3 1 Replaced coil 2009-01-02 20:00:04.700 4 1 Replaced LED 2002-08-20 20:00:04.700 5 2 Sealed leakage 2007-03-08 20:00:04.700 6 3 Replace connector 2004-05-16 20:00:04.700 I have another table that will show what each Part ID stands for, but that is not the problem I'm facing now. I'm required to write a query that returns the latest maintenance done on every parts. So in this case, my expected

Query to Return Top Items for Each Distinct Column Value

烈酒焚心 提交于 2019-12-02 04:58:42
If I have a table with the following fields ID, SomeFK, SomeTime How would I write a query return the latest/top 3 items (based on SomeTime ) for each SomeFK . So, the result might look like SomeFK Sometime 0 2012-07-05 0 2012-07-04 0 2012-07-03 1 2012-07-03 1 2012-07-02 1 2012-07-01 2 2012-07-03 2 2012-07-02 2 2012-07-01 ....etc.... Returning the latest items for a particular SomeFK is easy, but i just can't think how to do it for the above. I also feel it should be dead simple! EDIT: Apologies, I missed a key bit of information. this is for SQL2000, so ROW_NUMBER() can't be used! SELECT

PostgreSQL: Returning the most recent rows grouping by a foreign key column

血红的双手。 提交于 2019-12-02 04:47:05
I have a table that looks similar to this: credit +---------+----------------+-------------+--------------+-------------------------+ | id (PK) | person_id (FK) | transaction | total credit | date_time | +---------+----------------+-------------+--------------+-------------------------+ | 345 | 1 | -1.00 | 34.50 | 2018-08-29 12:00:00.000 | | 897 | 1 | 5.45 | 39.95 | 2018-08-29 12:34:00.000 | | 378 | 2 | 0.01 | 0.01 | 2018-08-29 08:00:00.000 | | 789 | 2 | 20.00 | 20.01 | 2018-08-29 09:00:00.000 | +---------+----------------+-------------+--------------+-------------------------+ How would I

SQL Server Join with Latest 2 Entries

风流意气都作罢 提交于 2019-12-02 04:30:00
I know the title of the post is bad but hear me out. A question like this arose the other day at work, and while I found a way around it, the problem still haunts me. Lets assume Stackoverflow has only 3 tables. Users ( username ) Comments ( comment, creationdate ) UsersCommentsJoin , this is the join table between the first 2 tables. Now lets say I want to make a query that would return the all the users with the last 2 most recent comments. So the result set would look like this. |username| most recent comment | second most recent comment| How on earth do I go about creating that query ? I

Firebird Query- Return first row each group

血红的双手。 提交于 2019-12-02 04:27:33
问题 In a firebird database with a table "Sales", I need to select the first sale of all customers. See below a sample that show the table and desired result of query. --------------------------------------- SALES --------------------------------------- ID CUSTOMERID DTHRSALE 1 25 01/04/16 09:32 2 30 02/04/16 11:22 3 25 05/04/16 08:10 4 31 07/03/16 10:22 5 22 01/02/16 12:30 6 22 10/01/16 08:45 Result: only first sale, based on sale date. ID CUSTOMERID DTHRSALE 1 25 01/04/16 09:32 2 30 02/04/16 11

SQL JOIN difficulty - seem to need a way of limiting rows in a join condition

∥☆過路亽.° 提交于 2019-12-02 03:58:39
问题 I'm a bit stuck trying to figure out how to design an Oracle query, and although there are similar questions here none of them seem to quite address the issues I'm facing. I have two tables and I want to join them: PROJECT table PROJECT_ID TITLE 101 First project 102 Second project 103 Third project 104 Fourth project 105 Fifth project EVENT table EVENT_ID PROJECT_FK EVENT_TYPE EVENT_DATE EVENT_DESC 201 101 301 2010-01-01 First event 202 101 301 2010-01-01 Second event 203 101 302 2010-01-02

Help to the way to write a query for the requirement

会有一股神秘感。 提交于 2019-12-02 02:23:00
I need to write a SQL-Server query but I don't know how to solve. I have a table RealtimeData with data: Time | Value 4/29/2009 12:00:00 AM | 3672.0000 4/29/2009 12:01:00 AM | 3645.0000 4/29/2009 12:02:00 AM | 3677.0000 4/29/2009 12:03:00 AM | 3634.0000 4/29/2009 12:04:00 AM | 3676.0000 // is EOD of day "4/29/2009" 4/30/2009 12:00:00 AM | 3671.0000 4/30/2009 12:01:00 AM | 3643.0000 4/30/2009 12:02:00 AM | 3672.0000 4/30/2009 12:03:00 AM | 3634.0000 4/30/2009 12:04:00 AM | 3632.0000 4/30/2009 12:05:00 AM | 3672.0000 // is EOD of day "4/30/2009" 5/1/2009 12:00:00 AM | 3673.0000 5/1/2009 12:01:00

Help to the way to write a query for the requirement

会有一股神秘感。 提交于 2019-12-02 02:15:29
问题 I need to write a SQL-Server query but I don't know how to solve. I have a table RealtimeData with data: Time | Value 4/29/2009 12:00:00 AM | 3672.0000 4/29/2009 12:01:00 AM | 3645.0000 4/29/2009 12:02:00 AM | 3677.0000 4/29/2009 12:03:00 AM | 3634.0000 4/29/2009 12:04:00 AM | 3676.0000 // is EOD of day "4/29/2009" 4/30/2009 12:00:00 AM | 3671.0000 4/30/2009 12:01:00 AM | 3643.0000 4/30/2009 12:02:00 AM | 3672.0000 4/30/2009 12:03:00 AM | 3634.0000 4/30/2009 12:04:00 AM | 3632.0000 4/30/2009