greatest-n-per-group

Top N Per Group with Multiple Table Joins

心已入冬 提交于 2019-11-27 15:11:53
问题 Based on my research, this is a very common problem which generally has a fairly simple solution. My task is to alter several queries from get all results into get top 3 per group . At first this was going well and I used several recommendations and answers from this site to achieve this (Most Viewed Products). However, I'm running into difficulty with my last one "Best Selling Products" because of multiple joins. Basically, I need to get all products in order by # highest sales per product

Retrieving the most recent records within a query

匆匆过客 提交于 2019-11-27 14:58:57
问题 I have the following tables: tblPerson: PersonID | Name --------------------- 1 | John Smith 2 | Jane Doe 3 | David Hoshi tblLocation: LocationID | Timestamp | PersonID | X | Y | Z | More Columns... --------------------------------------------------------------- 40 | Jan. 1st | 3 | 0 | 0 | 0 | More Info... 41 | Jan. 2nd | 1 | 1 | 1 | 0 | More Info... 42 | Jan. 2nd | 3 | 2 | 2 | 2 | More Info... 43 | Jan. 3rd | 3 | 4 | 4 | 4 | More Info... 44 | Jan. 5th | 2 | 0 | 0 | 0 | More Info... I can

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

試著忘記壹切 提交于 2019-11-27 14:15:24
问题 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. 回答1: Here's a solution to "top N per group" type queries. Note that you have to choose which 10 threads for a

MySQL Left Join + Min

徘徊边缘 提交于 2019-11-27 14:06:49
问题 Seemingly simple MySQL question, but I've never had to do this before.. I have two tables, items and prices, with a one-to-many relationship. Items Table id, name Prices Table id, item_id, price Where prices.item_id = items.id What I have so far: SELECT items.id, items.name, MIN(prices.price) FROM items LEFT JOIN prices ON items.id = prices.item_id GROUP BY items.id How do I also return the corresponding prices.id for that minimum price? Thanks! 回答1: This will return multiple records for a

Mysql select distinct

痞子三分冷 提交于 2019-11-27 12:21:04
I am trying to select of the duplicate rows in mysql table it's working fine for me but the problem is that it is not letting me select all the fields in that query , just letting me select the field name i used as distinct , lemme write the query for better understading mysql_query("SELECT DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id") mysql_query("SELECT * , DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id") 1st one is working fine now when i am trying to select all fields i am ending up with errors i am trying to select the latest of the duplicates let say ticket_id 127 is

Select a Column in SQL not in Group By

妖精的绣舞 提交于 2019-11-27 12:17:59
I have been trying to find some info on how to select a non-aggregate column that is not contained in the Group By statement in SQL, but nothing I've found so far seems to answer my question. I have a table with three columns that I want from it. One is a create date, one is a ID that groups the records by a particular Claim ID, and the final is the PK. I want to find the record that has the max creation date in each group of claim IDs. I am selecting the MAX(creation date), and Claim ID (cpe.fmgcms_cpeclaimid), and grouping by the Claim ID. But I need the PK from these records (cpe.fmgcms

SQL query to select distinct row with minimum value

吃可爱长大的小学妹 提交于 2019-11-27 10:56:15
I want an SQL statement to get the row with a minimum value. Consider this table: id game point 1 x 5 1 z 4 2 y 6 3 x 2 3 y 5 3 z 8 How do I select the ids that have the minimum value in the point column, grouped by game? Like the following: id game point 1 z 4 2 y 5 3 x 2 Ken Clark Use: SELECT tbl.* FROM TableName tbl INNER JOIN ( SELECT Id, MIN(Point) MinPoint FROM TableName GROUP BY Id ) tbl1 ON tbl1.id = tbl.id WHERE tbl1.MinPoint = tbl.Point Aspirant This will work select * from table where (id,point) IN (select id,min(point) from table group by id); As this is tagged with sql only, the

MySQL SELECT most frequent by group

百般思念 提交于 2019-11-27 09:16:44
How do I get the most frequently occurring category for each tag in MySQL? Ideally, I would want to simulate an aggregate function that would calculate the mode of a column. SELECT t.tag , s.category FROM tags t LEFT JOIN stuff s USING (id) ORDER BY tag; +------------------+----------+ | tag | category | +------------------+----------+ | automotive | 8 | | ba | 8 | | bamboo | 8 | | bamboo | 8 | | bamboo | 8 | | bamboo | 8 | | bamboo | 8 | | bamboo | 10 | | bamboo | 8 | | bamboo | 9 | | bamboo | 8 | | bamboo | 10 | | bamboo | 8 | | bamboo | 9 | | bamboo | 8 | | banana tree | 8 | | banana tree |

Order within group by?

假装没事ソ 提交于 2019-11-27 09:06:44
In my system, I have clients. Clients have programs. I want to display a list of clients, showing their most recent active (if it exists) program. Thus, we have something like this: SELECT * FROM clients AS client JOIN programs AS program ON client.id=program.client_id GROUP BY client.id ORDER BY program.close_date=0 DESC, program.close_date DESC close_date=0 means the program isn't closed. So it will put the non-closed programs first, and then the most recently closed programs next. Problem is, the order by doesn't work within the groups. It just kind of picks one of the programs at random.

Find out the nth-highest salary from table

本小妞迷上赌 提交于 2019-11-27 08:33:27
问题 name salary ----- ----- mohan 500 ram 1000 dinesh 5000 hareesh 6000 mallu 7500 manju 7500 praveen 10000 hari 10000 How would I find the nth-highest salary from the aforementioned table using Oracle? 回答1: you can use something like this.. this is what i have tested and then pasted here SELECT * FROM tblname WHERE salary = (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT DISTINCT( salary ) FROM tblname ORDER BY salary DESC) A WHERE rownum <= nth) B ORDER BY salary ASC) C WHERE rownum <= 1)