greatest-n-per-group

Grabbing the row with the highest value from each group?

∥☆過路亽.° 提交于 2019-12-12 13:18:35
问题 I have data that looks like this: Table Group: "A" , Color: "Blue", Count: "400" Group: "A" , Color: "Green", Count: "3" Group: "A" , Color: "Yellow", Count: "6" Group: "A" , Color: "Red", Count: "1" Group: "B" , Color: "Purple", Count: "243" Group: "B" , Color: "Green", Count: "2" Group: "B" , Color: "Yellow", Count: "7" How can I query this data to get for each group the most popular (by count) color. So the result would look like this: Result Group: "A", Color: "Blue" Group: "B", Color:

Return records distinct on one column but order by another column

拈花ヽ惹草 提交于 2019-12-12 09:15:41
问题 I am building a Rails 3 app with a pretty standard message model. I would like to return the most recently created message records for each unique conversation_id. It seems like a fairly simple task, but I have not been able to code or find a working solution. Admittedly, I am not super SQL savvy either (as I have gotten by with mainly Active Record queries thus far). Here is what I'm trying to accomplish. Sample messages table: | id | sender_id | receiver_id | conversation_id | subject |

SQL - selecting all rows with maximum value

青春壹個敷衍的年華 提交于 2019-12-12 08:47:38
问题 I have this SQL query: SELECT id, COUNT(*) AS price FROM (SELECT * FROM rt WHERE somecondition) AS st JOIN tt ON st.id = tt.id GROUP BY id; Now, I want to select all rows which have the maximum price of the table. I have tried this, which unfortunately returns no row at all: SELECT id, COUNT(*) AS price FROM (SELECT * FROM rt WHERE somecondition) AS st JOIN tt ON st.id = tt.id GROUP BY id HAVING price = MAX(price); I'm somewhat lost, does anybody have any pointers? 回答1: This looks fairly

MYSQL - Order By Id In DESC Order, Group By X

Deadly 提交于 2019-12-12 05:37:38
问题 For the past 4 hours I've been laser focused on this one problem, in a nut shell, I want to order this table by id in DESC order, grouped by ads_post_id (in DESC order based on id), with a LIMIT of 6 rows returned. Sample of database, id | ads_post_id --------------------------------------------------------------------------- 22 | 983314845117571 23 | 983314845117571 24 | 983314845117571 104 | 983314845117571 250 | 983314845117571 253 | 983314845117571 767 | 983314845117571 ------------------

Retrieve the highest value in a one-to-many-relationship

前提是你 提交于 2019-12-12 04:45:40
问题 I have a loan table and a properties table. One to many relationship. If the loan contains more than one property, I have to only retrieve the property that has the highest appraisal value. The following query SELECT l.loan_id,p_count FROM loans lo JOIN (SELECT loan_id, MAX(appraised_value) AS val COUNT(property_id) AS p_count FROM properties GROUP BY loan_id) AS pc ON pc.loan_id = lo.id gives me the output loan_id val p_count 817 914,000 2 But if I attempt to retrieve additional attributes

SELECT 3 records per user group by on two columns

回眸只為那壹抹淺笑 提交于 2019-12-12 04:03:09
问题 I've been stuck in a complex MySQL query. Here is my table: +--------------------------------------+ | id | user_id | category_id | post_id | +--------------------------------------+ | 1 | 23 | 5 | 213 | | 2 | 23 | 5 | 214 | | 3 | 23 | 5 | 215 | | 4 | 23 | 5 | 216 | | 5 | 23 | 6 | 217 | | 6 | 23 | 6 | 218 | | 7 | 23 | 6 | 219 | | 8 | 23 | 6 | 220 | | 9 | 55 | 13 | 221 | | 10 | 55 | 13 | 222 | | 11 | 55 | 16 | 223 | | 12 | 55 | 16 | 234 | | 13 | 55 | 22 | 235 | | 14 | 55 | 22 | 256 | | 15 | 55

DQL Doctrine query translation

空扰寡人 提交于 2019-12-12 03:56:45
问题 I have my scores table where I have multiple scores for 1 user. What I am trying to do is to select all highest scores for each user. I am trying to do the fallowing in Doctrine DQL: SELECT * FROM scores s1 LEFT OUTER JOIN scores s2 ON s1.user_id = s2.user_id AND ((s1.score < s2.score) OR (s1.score = s2.score AND s1.date_added < s2.date_added)) WHERE s2.score IS NULL ORDER BY s1.score DESC LIMIT 10 My current state is: $rowQuery = $this->getEntityManager()->createQuery(' SELECT s1 FROM

Postgresql: Show only the max value

时光毁灭记忆、已成空白 提交于 2019-12-12 03:48:40
问题 i have the following query that returns me the move, name of a container and the cicle of the container. The max value of the cicle on the container means that is the actual cicle of the same. select des.movimiento, des.equipo_identi, max(des.ciclo) as ciclo from publico.descarga des inner join publico.prioridad_movimiento primo on des.movimiento = primo.movimiento group by des.movimiento, des.equipo_identi, primo.prioridad order by primo.prioridad It returns me this movimiento | equipo

Select all columns from rows distinct on one column

只谈情不闲聊 提交于 2019-12-12 03:39:02
问题 I am using Netezza (based on PostgreSQL) and need to select all columns in a table for rows distinct on one column. A related question with answer can be found here, but it doesn't handle the case with all columns, going by that answer throws an error: select distinct on (some_field) table1.* from table1 order by some_field; Snippet from error with real data: "(" (at char 77) expecting '')'' 回答1: I don't think your code should throw an error in Postgres. However, it won't do what you expect

PostgreSQL select max from rows

和自甴很熟 提交于 2019-12-12 02:54:24
问题 I have the following data: CREATE TABLE offer ( id INTEGER, product_id VARCHAR, created_at TIMESTAMP, amount INTEGER, PRIMARY KEY (id)); INSERT INTO offer (id, product_id, created_at, amount) VALUES (1, '123', '2016-03-12', 990), (2, '136', '2016-02-01', 1056), (3, '111', '2016-01-01', 1000), (4, '123', '2016-01-02', 500); And I would like to get rows with the highest amount per product_id. If I take these previous rows I would like to get IDs: 2, 3 and 1 because row 1 contains a greater