greatest-n-per-group

MySQL subquery - Find only first record in a LEFT JOIN

断了今生、忘了曾经 提交于 2019-11-29 03:25:03
问题 I'm trying to display a list of member records, and I have a few tables I'm using to display what I need. That's the easy part. The part I need help with is with a table that has many records to each member record: Login history I want to display only the first row for each member record, that exists in the Login History table. Alternatively, I may want to flip flop and display the last record in the Login History table, as well. here's what I've got so far: SELECT m.memberid, m.membername, m

Select latest row for each group from oracle

回眸只為那壹抹淺笑 提交于 2019-11-29 02:00:21
I have a table with user comments in a guestbook. Columns are: id, user_id, title, comment, timestamp. I need to select the latest row for each user. I have tried to do this with group by but havent managed it because i cant select anything else in the same query where i group by user_id: SELECT user_id, MAX(ts) FROM comments GROUP BY user_id for example in this query i cant add to also select columns id, tilte and comment. How can this be done? You can use analytic functions SELECT * FROM (SELECT c.*, rank() over (partition by user_id order by ts desc) rnk FROM comments c) WHERE rnk = 1

MAX function in where clause mysql [duplicate]

强颜欢笑 提交于 2019-11-29 01:38:25
问题 This question already has answers here : Retrieving the last record in each group - MySQL (25 answers) Closed 23 days ago . How can I use max() function in where clause of a mysql query, I am trying: select firstName,Lastname,MAX(id) as max where id=max; this is giving me an error: Unknown column 'max' in 'where clause' Any Help? Thanks in advance. 回答1: We can't reference the result of an aggregate function (for example MAX() ) in a WHERE clause of the same SELECT . The normative pattern for

Nested Select statement in MYSQL join

别说谁变了你拦得住时间么 提交于 2019-11-29 00:48:17
问题 SELECT * FROM A JOIN B ON B.ID = A.ID AND B.Time = (SELECT max(Time) FROM B B2 WHERE B2.ID = B.ID) I am trying to join these two tables in MYSQL. Don't pay attention to that if the ID is unique then I wouldn't be trying to do this. I condensed the real solution to paint a simplified picture. I am trying to grab and join the table B on the max date for a certain record. This procedure is getting run by an SSIS package and is saying B2.ID is an unknown column. I do things like this frequently

How to make a SQL query for last transaction of every account?

狂风中的少年 提交于 2019-11-29 00:41:25
问题 Say I have a table "transactions" that has columns "acct_id" "trans_date" and "trans_type" and I want to filter this table so that I have just the last transaction for each account. Clearly I could do something like SELECT acct_id, max(trans_date) as trans_date FROM transactions GROUP BY acct_id; but then I lose my trans_type. I could then do a second SQL call with my list of dates and account id's and get my trans_type back but that feels very cludgy since it means either sending data back

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

大憨熊 提交于 2019-11-29 00:14:55
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 $21.55 Toy $89.95 I also want to get the fields id and name that belong to the above max price, for

Select multiple (non-aggregate function) columns with GROUP BY

ε祈祈猫儿з 提交于 2019-11-28 23:23:54
I am trying to select the max value from one column, while grouping by another non-unique id column which has multiple duplicate values. The original database looks something like: mukey | comppct_r | name | type 65789 | 20 | a | 7n 65789 | 15 | b | 8m 65789 | 1 | c | 1o 65790 | 10 | a | 7n 65790 | 26 | b | 8m 65790 | 5 | c | 1o ... This works just fine using: SELECT c.mukey, Max(c.comppct_r) AS ComponentPercent FROM c GROUP BY c.mukey; Which returns a table like: mukey | ComponentPercent 65789 | 20 65790 | 26 65791 | 50 65792 | 90 I want to be able to add other columns in without affecting

How do I select multiple items from each group in a mysql query?

孤街醉人 提交于 2019-11-28 22:08:44
I have some forum data of the form post(author, thread_id, text) For each author, I would like to select 10 distinct thread_ids associated with that author (there may be more than 10, and the number will vary by author). I'm thinking of using GROUP BY to group on 'author', but I cannot understand how to express the LIMIT on each group, and how to expand each group back into 10 rows. Here's a solution to "top N per group" type queries. Note that you have to choose which 10 threads for a given author you want. For this example, I'm assuming you want the most recent threads (and thread_id is an

SQL Left Join first match only

前提是你 提交于 2019-11-28 22:06:43
问题 I have a query against a large number of big tables (rows and columns) with a number of joins, however one of tables has some duplicate rows of data causing issues for my query. Since this is a read only realtime feed from another department I can't fix that data, however I am trying to prevent issues in my query from it. Given that, I need to add this crap data as a left join to my good query. The data set looks like: IDNo FirstName LastName ... -------------------------------------------

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

本小妞迷上赌 提交于 2019-11-28 15:25:26
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 name score date_added 1 Apple 15 1/4/09 2 Orange 15 1/4/09 3 Banana 15 1/4/09 I can get the data for a