greatest-n-per-group

Return rows that are max of one column in Postgresql

余生长醉 提交于 2019-12-11 07:28:02
问题 Sample data from my table test_table : date symbol value created_time 2010-01-09 symbol1 101 3847474847 2010-01-10 symbol1 102 3847474847 2010-01-10 symbol1 102.5 3847475500 2010-01-10 symbol2 204 3847474847 2010-01-11 symbol1 109 3847474847 2010-01-12 symbol1 105 3847474847 2010-01-12 symbol2 206 3847474847 Given the table above, I am trying to find the optimal index to put on the table (date, symbol, value and created_time should combined be unique) and the query to go along with it to

MYSQL, Max,Group by and Max

风流意气都作罢 提交于 2019-12-11 07:09:31
问题 I am having the following two table. 1.Movie Detail (Movie-ID,Movie_Name,Rating,Votes,Year) 2.Movie Genre (Movie-ID,Genre) I am using the following query to perform join and get the movie with highest rating in each genre. select Movie_Name, max(Rating) as Rating, Genre from movie_test inner join movie_genre where movie_test.Movie_ID = movie_genre.Movie_ID group by Genre In the output Rating and Genre are correct but the Movie_Name is incorrect. can anyone suggest what changes I should make

How to use SELECT DISTINCT ON with MySQL and Rails

旧城冷巷雨未停 提交于 2019-12-11 04:35:15
问题 I have quite a complex query to essentially select the cheapest delivery service price per delivery service . In order to get unique records per delivery service, I utilise the DISTINCT function in SQL. This query provides correct results: DeliveryServicePrice.active.select('DISTINCT ON (delivery_service_id) *').order('delivery_service_id, price ASC') (only a part of the query) However, this query only seems to work with PostgreSQL (which I think is strange considering PostgreSQL is a lot

How to include column which is not a part of group by

℡╲_俬逩灬. 提交于 2019-12-11 04:26:21
问题 How to get max of w_cost by v_id and also the final result set should include av_id . s_id sg_id r_cost w_cost av_id v_id 123 100 0.50 1.00 1 333 123 105 0.75 0.50 2 333 123 330 2.00 Null 3 888 If w_cost is NULL, r_cost should be taken. The final result should be: s_id v_id w_cost av_id 123 333 1.00 1 123 888 2.00 3 Basic query is SELECT t.s_id, sv.v_id, sv.w_cost, CASE WHEN sv.w_cost IS NULL THEN sv.r_cost::numeric ELSE sv.w_cost::numeric END AS cost FROM test t INNER JOIN stra_ven sv tmad

Mysql access main table inside right joined subquery

岁酱吖の 提交于 2019-12-11 04:18:17
问题 SELECT id,region FROM ads RIGHT JOIN (SELECT * FROM ads a2 WHERE a2.region=ads.region LIMIT 4) AS regions ON regions.id=ads.id Says "unknown column ads.region". How to access main table column region? I need to fetch 4 rows for each region 回答1: Use: SELECT a.id, a.region FROM ads a RIGHT JOIN (SELECT t.*, CASE WHEN @region = t.region THEN @rownum := @rownum + 1 ELSE @rownum := 1 END as rank, @region := t.region FROM ADS t JOIN (SELECT @rownum := 0, @region := 1) r ORDER BY t.region) b ON b.id

How do I select the max score from each distinct user in this table?

主宰稳场 提交于 2019-12-11 04:06:13
问题 I have the following table ( scores ): id user date score ---|-----|------------|-------- 1 | 10 | 11/01/2016 | 400 2 | 10 | 11/03/2016 | 450 5 | 17 | 10/03/2016 | 305 3 | 13 | 09/03/2016 | 120 4 | 17 | 11/03/2016 | 300 6 | 13 | 08/03/2016 | 120 7 | 13 | 11/12/2016 | 120 8 | 13 | 09/01/2016 | 110 I want to select max(score) for each distinct user, using date as a tie-breaker (in the event of a tie, the most recent record should be returned) such that the results look like the following (top

Rewrite self join to JPQL

假如想象 提交于 2019-12-11 02:55:50
问题 I need to convert this self join to JPQL: SELECT s1.* FROM site AS s1 JOIN (SELECT site_type, MAX(last_update_date) AS LastUpdate FROM site WHERE site.last_update_date > "2011-02-27 16:57:53" GROUP BY site_type) AS s2 ON s1.site_type = s2.site_type AND s1.last_update_date = s2.LastUpdate ORDER BY s1.last_update_date DESC Edit: the solution: SELECT s1 FROM Site s1 WHERE s1.lastUpdateDate = ( SELECT MAX(s2.lastUpdateDate) FROM Site s2 WHERE s1.siteType = s2.siteType) AND s1.lastUpdateDate >

SQL Query - Grouping with multiple tables

十年热恋 提交于 2019-12-11 02:46:49
问题 There are 2 tables involved in this question: Table1: |- Time Stamp -|- Special Number -|- Other Data -| |- 2011 -|- 1 -|- green -| |- 2010 -|- 1 -|- blue -| |- 2009 -|- 2 -|- yellow -| |- 2011 -|- 3 -|- red -| |- 2010 -|- 3 -|- orange -| |- 2009 -|- 4 -|- purple -| Table 2: |- Special Number -|- State (location) -| |- 1 -|- Hawaii -| |- 2 -|- Hawaii -| |- 3 -|- Alaska -| |- 4 -|- Alaska -| Table 2 relates the 'Special Number' to a 'State' Now then, the result I am trying to get would look

MySQL greatest-n-per-group trouble

☆樱花仙子☆ 提交于 2019-12-11 02:27:46
问题 Hey everyone. I believe this is a 'greatest-n-per-group' question but even after looking at several questions on StackOverflow, I'm unsure how to apply this to my situation... I'm using a MySQL database and have a basic blog-type system set up about Computer Applications... The tables look like this: POSTS post_id post_created post_type -- could be article, review, feature, whatever post_status -- 'a' approved or 'd' for draft APPS app_id app_name app_platform -- Windows, linux, unix, etc..

Deleting Rows: No Single Member Has More Than x Records

廉价感情. 提交于 2019-12-11 02:14:25
问题 I have a table structured like this: CREAT TABLE `member_logins` ( `id` bigint(10) unsigned not null auto_increment, `member_id` mediumint(8) unsigned not null, `date_created` datetime not null, PRIMARY KEY(`id`) ) ENGINE=InnoDB; I only want to keep 50 logins recorded for each member. So I'm looking for a way to DELETE rows on a per member basis, any rows past the most recent 50. Edit: I should have mentioned... This would be a nightly cron job. Not something that needs to be done in real