greatest-n-per-group

How to select the first N rows of each group?

百般思念 提交于 2019-11-27 07:37:19
I have two SQLite tables like this: AuthorId | AuthorName ---------------------- 1 | Alice 2 | Bob 3 | Carol ... | .... BookId | AuthorId | Title ---------------------------------- 1 | 1 | aaa1 2 | 1 | aaa2 3 | 1 | aaa3 4 | 2 | ddd1 5 | 2 | ddd2 ... | ... | ... 19 | 3 | fff1 20 | 3 | fff2 21 | 3 | fff3 22 | 3 | fff4 I want to make a SELECT query that will return the first N (e.g. two) rows for each AuthorId, ordering by Title ("Select the first two books of each author"). Sample output: BookId | AuthorId | AuthorName | Title ------------------------------------------ 1 | 1 | Alice | aaa1 2 | 1

GROUP BY in Postgres - no equality for JSON data type?

孤者浪人 提交于 2019-11-27 06:37:08
问题 I have the following data in a matches table: 5;{"Id":1,"Teams":[{"Name":"TeamA","Players":[{"Name":"AAA"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"CCC"},{"Name":"DDD"}]}],"TeamRank":[1,2]} 6;{"Id":2,"Teams":[{"Name":"TeamA","Players":[{"Name":"CCC"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"AAA"},{"Name":"DDD"}]}],"TeamRank":[1,2]} I want to select each last distinct Team in the table by their name. i.e. I want a query that will return: 6;{"Name":"TeamA","Players":[{

How to get the latest record in each group using GROUP BY? [duplicate]

冷暖自知 提交于 2019-11-27 06:33:48
This question already has an answer here: SQL select only rows with max value on a column [duplicate] 27 answers Let's say I have a table called messages with the columns: id | from_id | to_id | subject | message | timestamp I want to get the latest message from each user only, like you would see in your FaceBook inbox before you drill down into the actual thread. This query seems to get me close to the result I need: SELECT * FROM messages GROUP BY from_id However the query is giving me the oldest message from each user and not the newest. I can't figure this one out. You should find out last

Select corresponding to row from the same table SQL Server

岁酱吖の 提交于 2019-11-27 05:40:52
I want to get some column based on another column Example table: | BlilCode | BlilShortName | BatchWeigth | BillVersion | BlilMaxTime | +----------+---------------+-------------+-------------+-------------+ | 5502 | aaa | 1.00 | 1 | 360 | | 5502 | aaa | 2.00 | 2 | 240 | | 5510 | bbb | -32.94 | 2 | 360 | | 5510 | bbb | 1.00 | 1 | 360 | | 5510 | bbb | 36.37 | 3 | 3600 | but I want to get the rows where BillVersion is max for every BlilCode is max Expected result | BlilCode | BlilShortName | BatchWeigth | BillVersion | BlilMaxTime | +----------+---------------+-------------+-------------+--------

Laravel Eloquent group by most recent record

99封情书 提交于 2019-11-27 05:40:21
I'm trying to get the most recent record for a single customer on a table. Example: ID Customer City Amount 1 Cust001 City1 2 2 Cust001 City2 3 3 Cust001 City1 1 4 Cust001 City2 1 5 Cust001 City2 3 6 Cust001 City3 1 7 Cust001 City3 1 8 Cust002 City1 2 9 Cust002 City1 1 10 Cust002 City2 3 11 Cust002 City1 2 12 Cust002 City2 1 13 Cust002 City3 2 14 Cust002 City3 3 15 Cust003 City1 1 16 Cust003 City2 3 17 Cust003 City3 2 Please note that the table also has created_at and updated_at fields. I omitted those fields for simplicity. In the end I want my query to return for Cust001: ID Customer City

Sum results of a few queries and then find top 5 in SQL

ⅰ亾dé卋堺 提交于 2019-11-27 05:39:54
I have 3 queries: table: pageview SELECT event_id, count(*) AS pageviews FROM pageview GROUP BY event_id ORDER BY pageviews DESC, rand() LIMIT 1000 table: upvote SELECT event_id, count(*) AS upvotes FROM upvote GROUP BY event_id ORDER BY upvotes DESC, rand() LIMIT 1000 table: attending SELECT event_id, count(*) AS attendants FROM attending GROUP BY event_id ORDER BY attendants DESC, rand() LIMIT 1000 I'd like to combine the event_id s of all 3 queries ordered by amount and then choose the top 5. How do I do that? EDIT: HERE IS WHAT I DID TO MAKE IT HAPPEN: SELECT event_id, sum(amount) AS total

Optimize groupwise maximum query

陌路散爱 提交于 2019-11-27 05:28:43
select * from records where id in ( select max(id) from records group by option_id ) This query works fine even on millions of rows. However as you can see from the result of explain statement: QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=30218.84..31781.62 rows=620158 width=44) (actual time=1439.251..1443.458 rows=1057 loops=1) -> HashAggregate (cost=30218.41..30220.41 rows=200 width=4) (actual time=1439.203..1439.503 rows=1057 loops=1) -> HashAggregate (cost=30196.72.

Selecting all corresponding fields using MAX and GROUP BY

核能气质少年 提交于 2019-11-27 04:26:13
I have this table : And I would like to make a request that would return for each deal_id the row with the highest timestamp , and the corresponding status_id . So for this example, I would have returned 2 rows : 1226, 3, 2009-08-18 12:10:25 1227, 2, 2009-08-17 14:31:25 I tried to do it with this query SELECT deal_id, status_id, max(timestamp) FROM deal_status GROUP BY deal_id but it would return the wrong status_id : 1226, 1, 2009-08-18 12:10:25 1227, 1, 2009-08-17 14:31:25 without a single primary key field, I think your best bet is: select * from deal_status inner join (select deal_id as

Get most common value for each value of another column in SQL

我是研究僧i 提交于 2019-11-27 03:58:50
I have a table like this: Column | Type | Modifiers ---------+------+----------- country | text | food_id | int | eaten | date | And for each country, I want to get the food that is eaten most often. The best I can think of (I'm using postgres) is: CREATE TEMP TABLE counts AS SELECT country, food_id, count(*) as count FROM munch GROUP BY country, food_id; CREATE TEMP TABLE max_counts AS SELECT country, max(count) as max_count FROM counts GROUP BY country; SELECT country, max(food_id) FROM counts WHERE (country, count) IN (SELECT * from max_counts) GROUP BY country; In that last statement, the

SQL query to get most recent row for each instance of a given key

六眼飞鱼酱① 提交于 2019-11-27 03:10:27
I'm trying to get the ip, user, and most recent timestamp from a table which may contain both the current ip for a user and one or more prior ips. I'd like one row for each user containing the most recent ip and the associated timestamp. So if a table looks like this: username | ip | time_stamp --------------|----------|-------------- ted | 1.2.3.4 | 10 jerry | 5.6.6.7 | 12 ted | 8.8.8.8 | 30 I'd expect the output of the query to be: jerry | 5.6.6.7 | 12 ted | 8.8.8.8 | 30 Can I do this in a single sql query? In case it matters, the DBMS is Postgresql. Try this: Select u.[username] ,u.[ip] ,q.