greatest-n-per-group

SQL Query help - 10 records for each distinct column value

孤者浪人 提交于 2019-12-04 12:28:26
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 make; --- This returns only one record per make. select a.id,a.title,a.make from cars a left join (select

MYSQL query to find the all employees with nth highest salary

谁说我不能喝 提交于 2019-12-04 11:20:40
The two tables are salary_employee and employee employee_salary salary_id emp_id salary Employee emp_id | first_name | last_name | gender | email | mobile | dept_id | is_active Query to get the all employees who have nth highest salary where n =1,2,3,... any integer SELECT a.salary, b.first_name FROM employee_salary a JOIN employee b ON a.emp_id = b.emp_id WHERE a.salary = ( SELECT salary FROM employee_salary GROUP BY salary DESC LIMIT 1 OFFSET N-1 ) My Questions: 1) Is there any better and optimized way we can query this, 2) Is using LIMIT an good option 3) We have more options to calculate

How do I join to the latest record in the table?

霸气de小男生 提交于 2019-12-04 10:44:25
What I need done is simple... but its 3am and Im probably overlooking the obvious. Im coding a simple forum. One table stores the forum titles, descriptions, etc, while the other stores the posts. In the forum listing, that shows the list of all forums, I want to grab the latest post in each forum, and display the post subject, poster and post ID, and date. Simple. The only problem is, when I join to the posts table, it joins to the first record in the table, not the last, which would denote the last post in that forum. Here is the simplified query that gets a list of forums + data for the

select top n record from each group sqlite

Deadly 提交于 2019-12-04 07:56:31
I am trying to select top 2 records from a database table result that looks like this SubjectId | StudentId | Levelid | total ------------------------------------------ 1 | 1 | 1 | 89 1 | 2 | 1 | 77 1 | 3 | 1 | 61 2 | 4 | 1 | 60 2 | 5 | 1 | 55 2 | 6 | 1 | 45 i tried this query SELECT rv.subjectid, rv.total, rv.Studentid, rv.levelid FROM ResultView rv LEFT JOIN ResultView rv2 ON ( rv.subjectid = rv2.subjectid AND rv.total <= rv2.total ) GROUP BY rv.subjectid, rv.total, rv.Studentid HAVING COUNT( * ) <= 2 order by rv.subjectid desc but some subjects like where missing, i even tried the

Select most occurring value in MySQL

人走茶凉 提交于 2019-12-04 07:44:50
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? 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 GROUP BY thread_id This will tabulate the occurrences of user_id per thread SELECT thread_id, user_id, COUNT(*

How to write“greatest n per group” type query, but with additional conditions?

强颜欢笑 提交于 2019-12-04 07:09:05
I asked a question about making a "greatest n per group" type query yesterday (at the time not knowing it was called "greatest n per group") except I asked how to get the least per group. The table structure was as follows: type name value ===================== 1 item1 1 1 item2 20 2 item3 0 3 item4 5 3 item5 2 3 item6 50 I received several great answers, and the most helpful one was this: SELECT t1.type, t1.name, t1.value FROM mytable t1 LEFT JOIN mytable t2 ON t1.type = t2.type AND t1.value > t2.value WHERE t2.value IS NULL The above query results in this: type name value ===================

SQL - selecting all rows with maximum value

浪子不回头ぞ 提交于 2019-12-04 05:09:19
I have this SQL query: SELECT id, COUNT(*) AS price FROM (SELECT * FROM rt WHERE somecondition) AS st JOIN tt ON st.id = tt.id GROUP BY id; Now, I want to select all rows which have the maximum price of the table. I have tried this, which unfortunately returns no row at all: SELECT id, COUNT(*) AS price FROM (SELECT * FROM rt WHERE somecondition) AS st JOIN tt ON st.id = tt.id GROUP BY id HAVING price = MAX(price); I'm somewhat lost, does anybody have any pointers? user970780 This looks fairly simple to me: select * from <table> where <column name> in( SELECT MAX(column name) FROM table ) Try

Get the latest date for each record

試著忘記壹切 提交于 2019-12-04 05:03:48
问题 I have a History table (like a log) that records changes to parts: TransactionID Part ID Description Last Updated 1 1 Fixed O-ring 2006-03-14 20:00:04.700 2 2 Replaced coil 2009-01-02 20:00:04.700 3 1 Replaced coil 2009-01-02 20:00:04.700 4 1 Replaced LED 2002-08-20 20:00:04.700 5 2 Sealed leakage 2007-03-08 20:00:04.700 6 3 Replace connector 2004-05-16 20:00:04.700 I have another table that will show what each Part ID stands for, but that is not the problem I'm facing now. I'm required to

Latest datetime from unique mysql index

别说谁变了你拦得住时间么 提交于 2019-12-04 04:50:13
问题 I have a table. It has a pk of id and an index of [service, check, datetime]. id service check datetime score ---|-------|-------|----------|----- 1 | 1 | 4 |4/03/2009 | 399 2 | 2 | 4 |4/03/2009 | 522 3 | 1 | 5 |4/03/2009 | 244 4 | 2 | 5 |4/03/2009 | 555 5 | 1 | 4 |4/04/2009 | 111 6 | 2 | 4 |4/04/2009 | 322 7 | 1 | 5 |4/05/2009 | 455 8 | 2 | 5 |4/05/2009 | 675 Given a service 2 I need to select the rows for each unique check where it has the max date. So my result would look like this table.

Select N records for each category and order by X

扶醉桌前 提交于 2019-12-04 04:31:38
问题 I have a database table that contains blog posts. I want to show on the homepage one (or more) post for each category, ordering by date, for example. So my posts table looks like this: id | title | description | cat | filename | date How would I create such a query? I've thought to use group-by or a subselect but I'm not sure if it's a good thing for performance... the table has a large number of records. 回答1: MySQL doesn't support analytic functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE...),