greatest-n-per-group

How to update one table from another table based on some priority that depends on where clause?

ⅰ亾dé卋堺 提交于 2020-04-17 22:08:00
问题 I have table A with product_id,cost,year,quarter,... etc columns. I have another table B with product_id,base_cost,current_year,p_year,p_quarter,p_order columns. I want to write an update query to update A from B. My conditions are - WHERE A.product_id=B.product_id and A.year=B.current_year and (A.year=B.p_year and A.quarter>B.p_quarter) or A.year>B.p_year and A.cost=0; But the problem is, with these conditions if i have more than one rows in B then i only want to update from the row of B

How to update one table from another table based on some priority that depends on where clause?

為{幸葍}努か 提交于 2020-04-17 22:04:53
问题 I have table A with product_id,cost,year,quarter,... etc columns. I have another table B with product_id,base_cost,current_year,p_year,p_quarter,p_order columns. I want to write an update query to update A from B. My conditions are - WHERE A.product_id=B.product_id and A.year=B.current_year and (A.year=B.p_year and A.quarter>B.p_quarter) or A.year>B.p_year and A.cost=0; But the problem is, with these conditions if i have more than one rows in B then i only want to update from the row of B

How do I build a query in Ruby on Rails that joins on the max of a has_many relation only and includes a select filter on that relation?

ⅰ亾dé卋堺 提交于 2020-04-11 08:32:21
问题 I'm struggling how to have Ruby on Rails do this query right... in short: to join on a has_many relation but only via the most recent record in that relation and then can apply a filter/select on that relation. Here's a super simple variant that captures my struggle: Let's say I have a table of Employees and a table of Employments . An employee has_many employments . An employment has a status of :active or :inactive . class Employee < ActiveRecord::Base has_many :employments end class

Postgresql extract last row for each id

為{幸葍}努か 提交于 2020-03-12 07:12:25
问题 Suppose I've next data id date another_info 1 2014-02-01 kjkj 1 2014-03-11 ajskj 1 2014-05-13 kgfd 2 2014-02-01 SADA 3 2014-02-01 sfdg 3 2014-06-12 fdsA I want for each id extract last information: id date another_info 1 2014-05-13 kgfd 2 2014-02-01 SADA 3 2014-06-12 fdsA How could I manage that? 回答1: The most efficient way is to use Postgres' distinct on operator select distinct on (id) id, date, another_info from the_table order by id, date desc; If you want a solution that works across

How to get the last records for a combination of 2 columns?

柔情痞子 提交于 2020-03-02 08:04:33
问题 I have a situation which I think can be compared to services like CamelCamelCamel, Keepa and so on. Lets say I track the price of an article on each day for a couple of countries. So my table, lets call it Trend , would look something like this Id Created ArticleId Country Price ------------------------------------------------- 01 19/11/05 452 US 45.90 02 19/11/05 452 CA 52.99 03 19/11/05 452 MX 99.99 04 19/11/06 452 US 20.00 05 19/11/06 452 CA 25.00 06 19/11/06 452 MX 50.00 ... 97 19/11/05

How to get the last records for a combination of 2 columns?

只愿长相守 提交于 2020-03-02 08:03:19
问题 I have a situation which I think can be compared to services like CamelCamelCamel, Keepa and so on. Lets say I track the price of an article on each day for a couple of countries. So my table, lets call it Trend , would look something like this Id Created ArticleId Country Price ------------------------------------------------- 01 19/11/05 452 US 45.90 02 19/11/05 452 CA 52.99 03 19/11/05 452 MX 99.99 04 19/11/06 452 US 20.00 05 19/11/06 452 CA 25.00 06 19/11/06 452 MX 50.00 ... 97 19/11/05

Get best records of each name using mysql

北慕城南 提交于 2020-02-07 06:56:49
问题 I'm trying to make some kind of "top" for some game stadistics Table it's like this mapname authid country name time date weapon server I've got this query, but it's not ok SELECT *, min(time) FROM kz_nub15 WHERE mapname = '".$map."' GROUP BY name ASC ORDER BY time ASC LIMIT 15 I'm trying to get the best 15 times for a mapname, but only showing the best time of each name 回答1: How about trying it this way: SELECT * FROM kz_nub15 WHERE mapname = '".$map."' GROUP BY name ORDER BY time ASC LIMIT

How to select a max row for each group in SQL

﹥>﹥吖頭↗ 提交于 2020-01-30 04:35:10
问题 I want select countries with maximum value of 'Value' for a 'grpid'. Also already selected 'Country' should not be considered for other 'grpid' while checking the maximum. ( ie Country or grpid should not be repeated in the result ) SQL Fiddle Result: Country grpid Value Row_number US 49707 604456458 1 GB 5086 497654945 4 CA 909 353500201 10 JP 231 198291290 15 回答1: try this query instead, WITH OrderedOrders AS ( SELECT country,grpid,value,ROW_NUMBER() OVER(PARTITION BY country ORDER BY

MySQL retrieve latest record for Group

左心房为你撑大大i 提交于 2020-01-28 07:13:40
问题 I have a social networking site and am struggling with a query. I have a posts table that holds all of the users posts and then a post_comments table that holds all comments on a post. I am trying to find the latest comment by post from post_comments table. The post_comments table has the following columns: post_comment_id, post_id, writer_user_id, post_comment_content, datetime I have grouped the results by post_id like so: SELECT * FROM post_comments GROUP BY post_id This almost does what I

MySQL retrieve latest record for Group

你说的曾经没有我的故事 提交于 2020-01-28 07:12:29
问题 I have a social networking site and am struggling with a query. I have a posts table that holds all of the users posts and then a post_comments table that holds all comments on a post. I am trying to find the latest comment by post from post_comments table. The post_comments table has the following columns: post_comment_id, post_id, writer_user_id, post_comment_content, datetime I have grouped the results by post_id like so: SELECT * FROM post_comments GROUP BY post_id This almost does what I