groupwise-maximum

Get the latest records per Group By SQL

浪尽此生 提交于 2021-01-27 12:40:42
问题 I have the following table: ----------------------------------------------------------- ID oDate oName oItem oQty oRemarks ----------------------------------------------------------- 1 2016-01-01 A 001 2 2 2016-01-01 A 002 1 test 3 2016-01-01 B 001 3 4 2016-01-02 B 001 2 5 2016-01-02 C 001 2 6 2016-01-03 B 002 1 7 2016-01-03 B 001 4 ff. I want to get the latest record for each name. So the result should be like this: ----------------------------------------------------------- oDate oName

SQL select only rows with max value on a column [duplicate]

狂风中的少年 提交于 2020-01-25 07:07:08
问题 Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. This question already has answers here : Retrieving the last record in each group - MySQL (27 answers) Closed 10 months ago . I have this table for documents (simplified version here): +------+-------+--------------------------------------+ | id | rev | content | +------+-------+---------------------

SQL select only rows with max value on a column [duplicate]

北慕城南 提交于 2020-01-25 07:05:23
问题 Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. This question already has answers here : Retrieving the last record in each group - MySQL (27 answers) Closed 10 months ago . I have this table for documents (simplified version here): +------+-------+--------------------------------------+ | id | rev | content | +------+-------+---------------------

find maximum of set of columns for multiple rows in mysql query

馋奶兔 提交于 2020-01-03 05:56:29
问题 I have a table with the following fields and data. RefNo Year Month Code 1 2010 7 A 2 2009 10 B 3 2010 8 A 4 2010 5 B From this i've to find out the maximum(year, month) for a particular code. The result must be 3 2010 8 A 1 2010 7 B Please help me solve this. 回答1: SELECT * FROM ( SELECT ROW_NUMBER() OVER (PARTITION BY Code ORDER BY Year DESC, Month DESC) AS row_number, * FROM myTable ) AS orderedTable WHERE row_number = 1 EDIT And now for a version that WORKS in MySQL... SELECT * FROM

Optimize groupwise maximum query

て烟熏妆下的殇ゞ 提交于 2019-12-17 07:48:30
问题 select * from records where id in ( select max(id) from records group by option_id ) This query works fine even on millions of rows. However as you can see from the result of explain statement: QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=30218.84..31781.62 rows=620158 width=44) (actual time=1439.251..1443.458 rows=1057 loops=1) -> HashAggregate (cost=30218.41..30220.41

Finding running maximum by group

坚强是说给别人听的谎言 提交于 2019-12-17 05:14:06
问题 I need to find a running maximum of a variable by group using R. The variable is sorted by time within group using df[order(df$group, df$time),] . My variable has some NA's but I can deal with it by replacing them with zeros for this computation. this is how the data frame df looks like: (df <- structure(list(var = c(5L, 2L, 3L, 4L, 0L, 3L, 6L, 4L, 8L, 4L), group = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("a", "b"), class = "factor"), time = c(1L, 2L, 3L, 4L, 5L, 1L, 2L

How can I include third column in a SELECT with MIN

拜拜、爱过 提交于 2019-12-13 04:17:33
问题 In this query, I also want to SELECT a third column, wp_postmeta.post_id , in the same query. I can't figure out how to do this without appending it via a JOIN which involves subquerying. Isn't there a simpler easier way, without the use of subqueries? (Willing to concede on subqueries if the solution is efficient and not too resource-intensive.) MariaDB 10.1 create table wp_posts ( ID integer primary key auto_increment, post_title varchar(30), post_type varchar(30) ); ✓ create table wp

Looking to create a subquery that displays only one column MYSQL

自古美人都是妖i 提交于 2019-12-11 19:04:45
问题 This database is for a book loan system. Where if a student takes a copy of a book the system accepts there student number and gives them a return date for the book. These are the two tables I am using loan table: CREATE TABLE loan ( `code` INT NOT NULL, `no` INT NOT NULL, taken DATE NOT NULL, due DATE NOT NULL, `return` DATE NULL, CONSTRAINT pri_loan PRIMARY KEY (taken), CONSTRAINT for_loan FOREIGN KEY (`code`) REFERENCES copy (`code`), FOREIGN KEY (`no`) REFERENCES student (`no`)); student

Query to rank rows in groups

岁酱吖の 提交于 2019-12-02 02:54:25
问题 I'm using Apache Derby 10.10. I have a list of participants and would like to calculate their rank in their country, like this: | Country | Participant | Points | country_rank | |----------------|---------------------|--------|--------------| | Australia | Bridget Ciriac | 1 | 1 | | Australia | Austin Bjorklun | 4 | 2 | | Australia | Carrol Motto | 7 | 3 | | Australia | Valeria Seligma | 8 | 4 | | Australia | Desmond Miyamot | 27 | 5 | | Australia | Maryjane Digma | 33 | 6 | | Australia |

Query to rank rows in groups

自作多情 提交于 2019-12-02 01:08:48
I'm using Apache Derby 10.10. I have a list of participants and would like to calculate their rank in their country, like this: | Country | Participant | Points | country_rank | |----------------|---------------------|--------|--------------| | Australia | Bridget Ciriac | 1 | 1 | | Australia | Austin Bjorklun | 4 | 2 | | Australia | Carrol Motto | 7 | 3 | | Australia | Valeria Seligma | 8 | 4 | | Australia | Desmond Miyamot | 27 | 5 | | Australia | Maryjane Digma | 33 | 6 | | Australia | Kena Elmendor | 38 | 7 | | Australia | Emmie Hicke | 39 | 8 | | Australia | Kaitlyn Mund | 50 | 9 | |