greatest-n-per-group

MySQL: Limiting number of results received based on a column value | Combining queries

时间秒杀一切 提交于 2019-12-01 01:08:26
问题 I've done research on this problem, but am having trouble finding a solution. I have the following query that gives me a list of "some_id"s: SELECT some_id FROM example GROUP BY some_id And I have the following query that will get a list of the 5 most recent entries for a row that has "some_id" equal to a number. SELECT * FROM example WHERE some_id = 1 ORDER BY last_modified DESC LIMIT 5 How can I get the the top 5 most recent entries from the table "example" for each "some_id", using only

how to get second highest salary department wise without using analytical functions?

空扰寡人 提交于 2019-12-01 00:52:04
Suppose we have 3 employees in each department.we have total 3 departments . Below is the sample source table Emp deptno salary A 10 1000 B 10 2000 C 10 3000 D 20 7000 E 20 9000 F 20 8000 G 30 17000 H 30 15000 I 30 30000 Output B 10 2000 F 20 8000 G 30 17000 With using analytic function dense_rank we can achive the second highest salary dept wise. Can we achieve this without using ANY analytic function ??? Is Max() is also analytic function ?? It is a pain, but you can do it. The following query gets the second highest salary: select t.deptno, max(t.salary) as maxs from table t where t.salary

Finding the highest n values of each group in MySQL

烂漫一生 提交于 2019-11-30 23:43:29
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 Server and using ORDER BY... LIMIT only returns the highest N results overall, not per lane. Since I use a

SQL Query, Selecting 5 most recent in each group

三世轮回 提交于 2019-11-30 20:14:19
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `language_id` int(11) unsigned NOT NULL, `title` varchar(60) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `time_posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 language_id refers to what language the record is in. What I would like to do is retrieve a list of the five most recent (ORDER BY time_posted DESC LIMIT 5) records in each language_id . I could do this in a loop within PHP with a number of different SQL queries but I

T-SQL select rows by oldest date and unique category

丶灬走出姿态 提交于 2019-11-30 20:07:04
问题 I'm using Microsoft SQL. I have a table that contains information stored by two different categories and a date. For example: ID Cat1 Cat2 Date/Time Data 1 1 A 11:00 456 2 1 B 11:01 789 3 1 A 11:01 123 4 2 A 11:05 987 5 2 B 11:06 654 6 1 A 11:06 321 I want to extract one line for each unique combination of Cat1 and Cat2 and I need the line with the oldest date. In the above I want ID = 1, 2, 4, and 5. Thanks 回答1: Have a look at row_number() on MSDN. SELECT * FROM ( SELECT *, ROW_NUMBER() OVER

Getting the latest n records for each group

耗尽温柔 提交于 2019-11-30 19:55:41
问题 Lets say I have the following table: id coulmn_id value date 1 10 'a' 2016-04-01 1 11 'b' 2015-10-02 1 12 'a' 2016-07-03 1 13 'a' 2015-11-11 2 11 'c' 2016-01-10 2 23 'd' 2016-01-11 3 11 'c' 2016-01-09 3 111 'd' 2016-01-11 3 222 'c' 2016-01-10 3 333 'd' 2016-01-11 for n = 3, I want to get the latest n records<=3 for each id. So I will have the following output: id column_id value date 1 10 'a' 2016-04-01 1 12 'a' 2016-07-03 1 13 'a' 2015-11-11 2 11 'c' 2016-01-10 2 23 'd' 2016-01-11 3 111 'd'

MySQL return first row of a joined table

ε祈祈猫儿з 提交于 2019-11-30 18:44:34
I have two tables (country & ducks) where the country table has every country in the world and the ducks table has a list of ducks with a country_id field to link to the main country. I'm trying to get a list of only countries with at least one duck in it and with that a single matching record from the ducks table for the highest rated duck within that country. So far I have: SELECT * FROM country c INNER JOIN ducks d ON c.id = d.country_id ORDER BY c.country ASC, d.rating DESC This returns a list of every duck rather than just one per country. I'd be grateful if anyone can point me in the

How to select single row based on the max value in multiple rows [duplicate]

徘徊边缘 提交于 2019-11-30 17:25:39
Possible Duplicate: SQL: Find the max record per group I have a table with four columns as such: name major minor revision p1 0 4 3 p1 1 0 0 p1 1 1 4 p2 1 1 1 p2 2 5 0 p3 3 4 4 This is basically ca table containing records for each version of a program. I want to do a select to get all of the programs and their latest version so the results would look like this: name major minor revision p1 1 1 4 p2 2 5 0 p3 3 4 4 I can't just group by the name and get the max of each column because then i would just end up with the highest number from each column, but not the specific row with the highest

retrieve the most recent record for each customer

回眸只為那壹抹淺笑 提交于 2019-11-30 16:20:32
I have this data: ID NAME DATE 3 JOHN 2011-08-08 2 YOKO 2010-07-07 1 JOHN 2009-06-06 Code (for SQL Server 2005): DECLARE @TESTABLE TABLE (id int, name char(4), date smalldatetime) INSERT INTO @TESTABLE VALUES (3, 'JOHN', '2011-08-08') INSERT INTO @TESTABLE VALUES (2, 'YOKO', '2010-07-07') INSERT INTO @TESTABLE VALUES (1, 'JOHN', '2009-06-06') I want to get, for each NAME, the ID that has the most recent DATE. Like this: 3 JOHN 2011-08-08 2 YOKO 2010-07-07 What is the most elegant way of accomplishing this? ;WITH x AS ( SELECT ID, NAME, [DATE], rn = ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY

DQL Select every rows having one column's MAX value

南笙酒味 提交于 2019-11-30 15:51:53
问题 Working with Symfony 2 and Doctrine, I'm searching for a way to select every rows having the max value in a specific column. Right now, I'm doing it in two queries: One to get the max value of the column in the table Then I select rows having this value. I'm sure this can be done with one query. Searching, I have found this answer in a thread, that seems to be what I am searching for, but in SQL. So according to the answer's first solution, the query I'm trying to build would be something