greatest-n-per-group

Highest appearance of a value within each group

浪尽此生 提交于 2019-12-11 00:07:21
问题 I'm having a table with an user-ID and the Webside-IDs he navigated through. The table looks like this: | user-ID | website-ID | | 1 | 1 | | 1 | 2 | | 1 | 1 | | 1 | 4 | | 2 | 1 | | 2 | 2 | | 2 | 5 | | 2 | 2 | I'd like to select the website-ID each user has been into the most. As you can see, a min , max or avg won't work here. Any ideas? 回答1: This may look complicated but it will only aggregate the data once only , then pass through it ranking the results and choosing only the first one

mysql query - blog posts and comments with limit

元气小坏坏 提交于 2019-12-10 23:40:10
问题 I have 2 tables: comments & posts. I'd like to display a list of 15 posts and maximum 2 most recent comments under each blog post entry using mysql. the database scheme looks like this posts_table: post_id, post_txt, post_timestamp comments_table: post_id, comment_txt, comment_timestamp how should the mysql query look in order to select 15 posts and their related comments (max 2 most recent ones per post)? thanks 回答1: MySQL LIMIT SELECT * FROM posts_table LIMIT 0, 15 And to pull the most

SQL Select only rows with Max Value on a Column FILTERED by Column

て烟熏妆下的殇ゞ 提交于 2019-12-10 22:58:21
问题 This is a followup question to the excellent answer: SQL Select only rows with Max Value on a Column SQLFiddle http://sqlfiddle.com/#!2/3077f/2 Table "yourtable": | id | val | ignore | content | ------------------------------- | 1 | 10 | 0 | A | | 1 | 20 | 0 | B | | 1 | 30 | 1 | C | | 2 | 40 | 0 | D | | 2 | 50 | 0 | E | | 2 | 60 | 1 | F | When looking for max val per id, following sql is used: select yt1.* from yourtable yt1 left outer join yourtable yt2 on (yt1.id = yt2.id and yt1.val < yt2

Getting last 6 records for each team in a MySQL database

天大地大妈咪最大 提交于 2019-12-10 21:04:36
问题 I have a MySQL database containing soccer results and want to retrieve just a specific subset of that data. The data consists of one table containing MatchDate, HomeTeam, AwayTeam, HomeGoals, AwayGoals How can I retrieve a subset of this data that contains the last 6 matches that each team has been involved in? Whilst I can do this for a single team, how do I get a single subset that contains the last 6 matches for each team in the table? (I am not worried that the subset may contain some

Group by sku, max date SQL

非 Y 不嫁゛ 提交于 2019-12-10 20:56:08
问题 I know this is asked quite a bit here, and I have tried to use other examples to incorporate into my own, but I can't seem to make this work. I have columns for sku, date, and cost, and I want to view all 3 columns, but only by max date, grouped by sku. Currently: Sku Date Cost 1 06/24/15 .01 1 02/22/14 .02 2 06/24/15 .04 2 02/22/14 .05 Need: Sku Date Cost 1 06/24/15 .01 2 06/24/15 .04 This is what my SQL looks like: SELECT dbo_SKU.PROD_CODE AS Sku, dbo_LOTS.REC_DATE AS [Last Date], dbo_LOT

Limit results on an GROUP_CONCAT() or INNER JOIN

百般思念 提交于 2019-12-10 20:44:06
问题 I've perused extensively the other threads talking about limits on group_concat() and inner joins but haven't found my answer, so I guess I'll go ahead and ask it: I'm developing an existing photo community site. I want to retrieve members who have their birthday on a given day (today) and then retrieve each member's 5 most highly rated photos. But I also only want the 10 "most favorite" birthday members (ie with the highest favorite count). Here's what I have: SELECT users.user_id, users

mysql how to get 2nd highest value with group by and in a left join

与世无争的帅哥 提交于 2019-12-10 20:34:39
问题 (select id from owner where date_format(auction_date,'%Y-%m-%d %H:%i:00') = date_format(NOW(),'%Y-%m-%d %H:%i:00')) as a left join (select owner_id,max(nb) as maxbid from auction group by owner_id) as b on a.id=b.owner_id left join (select owner_id,max(mb) as maxautobid from auction group by owner_id) as c on a.id=c.owner_id For the second left join statement, i'm able to get the highest mb value. Can someone help me add a third left join statement so that i can get the second highest mb

MySQL get last date records from multiple

五迷三道 提交于 2019-12-10 20:18:45
问题 I have the following in my db: ID datetime type value 1094 2013-09-25 09:11:18 HDD 89% 1094 2013-09-25 09:11:18 CPU 55% 1093 2013-09-25 09:11:18 HDD 77% 1093 2013-09-25 09:11:18 CPU 55% 1094 2013-09-25 08:21:28 HDD 85% 1094 2013-09-25 08:21:28 CPU 11% 1093 2013-09-25 07:04:00 HDD 76% I want to get the last date for every ID and type record: 1094 2013-09-25 09:11:18 HDD 89% 1094 2013-09-25 09:11:18 CPU 55% 1093 2013-09-25 09:11:18 HDD 77% 1093 2013-09-25 09:11:18 CPU 55% 回答1: You can use GROUP

Finding the record with maximum value in SQL

≯℡__Kan透↙ 提交于 2019-12-10 20:18:29
问题 I have the following table: Class, Name, Score 1, Anna, 34 1, Andy, 80 2, Brooke, 90 2, Brad, 70 3, Charles, 67 3, Christina, 66 How to I find the 'Name' with maximum 'Score' in each 'Class' ? Required Output: Class, Name, Score 1, Andy, 80 2, Brooke, 90 3, Charles, 67 This is for MySQL. 回答1: WITH ClassScores AS ( SELECT 1 AS class, 'Anna' AS name, 34 AS score UNION SELECT 1, 'Andy', 80 UNION SELECT 2, 'Brooke', 90 UNION SELECT 2, 'Brad', 70 UNION SELECT 3, 'Charles', 67 UNION SELECT 3,

Limit SQL query to only the top two counts per group [duplicate]

可紊 提交于 2019-12-10 19:32:34
问题 This question already has answers here : Get top results for each group (in Oracle) (5 answers) Closed last year . If I have a DB table called FAVORITE_FLAVOR where each row has a user's favorite flavor of ice cream. User ID | Flavor | State 1 | Chocolate | CA 2 | Vanilla | ND 3 | Chocolate | CA 4 | Rocky Road | CA 5 | vanilla | CA 6 | Vanilla | CA 7 | Vanilla | CA Now, if I want to query the 2 most popular flavors in each state (normalizing capitalization and whitespace), I could query: