greatest-n-per-group

MYSQL - Join most recent matching record from one table to another

本小妞迷上赌 提交于 2019-12-17 19:37:03
问题 I have two tables that look like this: Table: cases id name status case_no Table: notes id case_id note_date notes I'd like to be able to create a query that grabs the data from the cases table and only the most recent entry from the notes table for each row in the cases table. So far I'm having no luck at all. Any pointers would be greatly appreciated 回答1: This will return only the cases with notes attached: SELECT c.*, x.* FROM CASES c JOIN NOTES x ON x.case_id = c.case_id JOIN (SELECT n

MySQL select top X records for each individual in table

梦想与她 提交于 2019-12-17 18:14:37
问题 Is there a better way to get multiple "top X" results from a MySQL table? I'm able to accomplish this easily with a union when the number of different foo is small: (SELECT foo,score FROM tablebar WHERE (foo = 'abc') ORDER BY score DESC LIMIT 10) UNION (SELECT foo,score FROM tablebar WHERE (foo = 'def') ORDER BY score DESC LIMIT 10) I could obviously keep adding unions for each value of foo. However, this isn't practical when there are 500+ different values for foo and I need the top X of

How to select more than 1 record per day?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 16:52:59
问题 This is a postgresql problem. PostgreSQL 8.3.3 on i686-redhat-linux-gnu, compiled by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-9). The table looks like: date_time other_column 2012-11-01 00:00:00 ... 2012-11-02 01:00:00 ... 2012-11-02 02:00:00 ... 2012-11-02 03:00:00 ... 2012-11-02 04:00:00 ... 2012-11-03 05:00:00 ... 2012-11-03 06:00:00 ... 2012-11-05 00:00:00 ... 2012-11-07 00:00:00 ... 2012-11-07 00:00:00 ... ... I want to select at most 3 records per day from a specific date range. For

SQL Query: Return Max value record of a Group

我的梦境 提交于 2019-12-17 14:57:04
问题 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

Select one row per index value with max column value

自闭症网瘾萝莉.ら 提交于 2019-12-17 14:55:06
问题 With a table setup with the following fields: SKU, EVENTSTARTDATE, EVENTENDDATE, PRICE, (...etc...) and housing thousands of rows here is example data (dates are YYMMDD, century code excluded): 1111111, 101224, 101231, 10.99 1111111, 110208, 110220, 9.99 1111111, 110301, 110331, 8.99 2222222, 101112, 101128, 15.99 2222222, 101201, 110102, 14.99 etc I'd like to have a SELECT statement return one row per SKU with the maximum EVENTSTARTDATE without having a WHERE clause isolating a specific SKU

MySQL groupwise MAX() returns unexpected results

无人久伴 提交于 2019-12-17 09:58:10
问题 TABLE: LOAN Loan_no Amount SSS_no Loan_date 7 700.00 0104849222 2010-01-03 8 200.00 0104849222 2010-02-28 9 300.00 0119611199 2010-11-18 10 150.00 3317131410 2012-11-28 11 600.00 0104849222 2011-01-03 14 175.00 3317131410 2012-12-05 15 260.00 3317131410 2013-02-08 16 230.00 0104849222 2013-03-06 17 265.00 0119611199 2011-04-30 18 455.00 3317131410 2013-03-10 DESIRED RESULTS: I would want to retrieve the latest loan availed off by each person (identified by their SSS number). The results

Optimize groupwise maximum query

て烟熏妆下的殇ゞ 提交于 2019-12-17 07:48:30
问题 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

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

∥☆過路亽.° 提交于 2019-12-17 07:21:33
问题 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

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

帅比萌擦擦* 提交于 2019-12-17 07:19:19
问题 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

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

末鹿安然 提交于 2019-12-17 07:11:36
问题 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