greatest-n-per-group

Select most occurring value in MySQL

爷,独闯天下 提交于 2020-01-22 23:07:39
问题 I'm looking for a way to select the most occurring value, e.g. the person who posted most for each thread; SELECT MOST_OCCURRING(user_id) FROM thread_posts GROUP BY thread_id Is there a good way to do this? 回答1: If you want a count on a per thread basis, I think you can use a nested query; grouping by thread first and then by user: SELECT thread_id AS tid, (SELECT user_id FROM thread_posts WHERE thread_id = tid GROUP BY user_id ORDER BY COUNT(*) DESC LIMIT 0,1) AS topUser FROM thread_posts

Select most occurring value in MySQL

柔情痞子 提交于 2020-01-22 23:07:04
问题 I'm looking for a way to select the most occurring value, e.g. the person who posted most for each thread; SELECT MOST_OCCURRING(user_id) FROM thread_posts GROUP BY thread_id Is there a good way to do this? 回答1: If you want a count on a per thread basis, I think you can use a nested query; grouping by thread first and then by user: SELECT thread_id AS tid, (SELECT user_id FROM thread_posts WHERE thread_id = tid GROUP BY user_id ORDER BY COUNT(*) DESC LIMIT 0,1) AS topUser FROM thread_posts

Finding the highest n values of each group in MySQL

為{幸葍}努か 提交于 2020-01-21 05:30:14
问题 I have some data formatted like this: Lane Series 1 680 1 685 1 688 2 666 2 425 2 775 ... And I'd like to grab the highest n series per lane (let's say 2 for the sake of this example, but it could be many more than that) So the output should be: Lane Series 1 688 1 685 2 775 2 666 Getting the highest series per lane is easy, but I can't seem to find a way to get the highest 2 results. I use a MAX aggregate function with a GROUP BY to get the MAX, but there's no "TOP N" function as in SQL

Selecting one random data from a column from multiple rows in oracle

坚强是说给别人听的谎言 提交于 2020-01-17 08:21:10
问题 I am creating a view that needs to select only one random row for each customer. Something like: select c.name, p.number from customers c, phone_numbers p where p.customer_id = c.id If this query returns: NAME NUMBER --------- ------ Customer1 1 Customer1 2 Customer1 3 Customer2 4 Customer2 5 Customer3 6 I need it to be something like: NAME NUMBER --------- ------ Customer1 1 Customer2 4 Customer3 6 Rownum wont work because it will select only the first from all 6 records, and i need the

Different result query when use mysql and mariadb

自闭症网瘾萝莉.ら 提交于 2020-01-16 04:07:11
问题 Here is my problem: My database have table Book, Post. Each book has many post Table posts has field ' book_id ', that is foreign key reference table Book primary key (id). This is my index page. The idea is to get latest post from each book and order by published date. When I code on localhost, every thing is OK. I can get latest post from each book and order by publish date. But when I deploy it in vps. It didn't get latest post, it get first post from each book. I didn't have any

Selecting top N records per group in DynamoDB

倖福魔咒の 提交于 2020-01-15 11:59:25
问题 Is NoSQL in general, and DynamoDB in particular, well suited to performing greatest-n-per-group type queries, as compared to MySQL? 回答1: DynamoDB support only 2 index and can only be queried efficiently on these. hash key range key ( optional ) Using DynamoDB to find the biggest values in a random "row" is not a good idea at all. Querying on a random row implies scanning the whole dataset which will cost you a lot of money. Nonetheless, if your data is properly modeled, query method may be

Select most recent results based on composite of fields

孤人 提交于 2020-01-14 19:38:08
问题 I'm using MySQL, but I think this is a basic SQL question. I don't know how else to ask but to give an example. Say I have this data in my table: id date_time foreign_key key value 1 2010-01-01 00:00:00 1 'temperature' 84 2 2010-01-01 00:00:01 1 'humidity' 34 3 2010-01-01 00:00:02 2 'temperature' 45 4 2010-01-01 00:00:03 2 'humidity' 23 5 2010-01-01 00:00:04 2 'dew_point' 78 6 2010-01-01 00:00:05 3 'temperature' 57 7 2010-01-01 00:00:06 3 'humidity' 41 8 2010-01-01 00:00:07 4 'temperature' 19

SQL return n rows per row value

岁酱吖の 提交于 2020-01-14 04:24:33
问题 Greetings SQL people of all nations. Simple question, hopefully a simple answer. I have an Oracle database table with persons' information. Columns are: FirstName, LastName, BirthDate, BirthCountry Let's say in this table I have 1500 persons born in Aruba (BirthCountry = "Aruba"), 678 Botswanans (BirthCountry = "Botswana"), 13338 Canadians (BirthCountry = "Canadia"). What query would I need to write extract a sample batch of 10 records from each country? It doesn't matter which 10, just as

SQL Query help - 10 records for each distinct column value

守給你的承諾、 提交于 2020-01-13 04:50:11
问题 I have a cars table which contains car listings. The table structure looks something like: cars - id - title - make - year I would like a query which returns 10 cars of each make. Something equivalent to the following pseudo code: car_makes = select distinct make from cars for each make in car_makes select id, title, make from clcars where make = $make limit 10; end Here's what I've tried unsuccessfully: select id,title,make from cars where make in (select distinct make from cars) group by

MySQL: how to get x number of results per grouping [duplicate]

ε祈祈猫儿з 提交于 2020-01-11 20:00:27
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: mysql: Using LIMIT within GROUP BY to get N results per group? I have a two tables: Items Categories Each item belongs to a category. What I want to do is select 5 items per category but say 20 items in total. SELECT item_id, item_name, items.catid FROM items, categories WHERE items.catid = categories.catid GROUP BY items.catid LIMIT 0,5 //5 per category group Edit: if there are more than 5 items per category -