greatest-n-per-group

Subset by group with data.table

喜你入骨 提交于 2019-12-12 02:49:55
问题 Assume I have a data table containing some baseball players: library(plyr) library(data.table) bdt <- as.data.table(baseball) For each player (given by id), I want to find the row corresponding to the year in which they played the most games. This is straightforward in plyr: ddply(baseball, "id", subset, g == max(g)) What's the equivalent code for data.table? I tried: setkey(bdt, "id") bdt[g == max(g)] # only one row bdt[g == max(g), by = id] # Error: 'by' or 'keyby' is supplied but not j bdt

UPDATE from result of SELECT

三世轮回 提交于 2019-12-12 02:31:57
问题 I have a problem with updating my table with a select from another table. Here is my description: Table part has the following fields: part_num PK active notes weight Table importedDocument has the following fields: part_num PK active notes weight quantity PK condition_id PK part_num in part is unique, but part_num in importedDocument is not. Every part_num that is in importedDocument is also in part . What I want to do is to get DISTINCT part_num from importedDocuemnt , and with this result

Select up to x rows of each group

[亡魂溺海] 提交于 2019-12-12 02:23:43
问题 With a table of: id | name | job | rank 01 john teacher 4 02 mark teacher 2 03 phil plummer 1 04 dave teacher 7 05 jim plummer 9 06 bill plummer 2 How can I select up to 2 rows of each job (if possible sorted by rank ASC in each group, so that the lowest two ranking of each group get picked). The result I'd be looking for is: 02 mark teacher 2 01 john teacher 4 03 phil plummer 1 06 bill plummer 2 This basically groups by job, with a limit to 2 and sorted by rank. I've been trying with GROUP

Query one document per association from MongoDB

别来无恙 提交于 2019-12-12 02:21:28
问题 I'm investigating how MongoDB would work for us. One of the most used queries is used to get latest (or from a given time) measurements for each station. There is thousands of stations and each station has tens of thousands of measurements. So we plan to have one collection for stations and another for measurements. In SQL we would do the query with SELECT * FROM measurements INNER JOIN ( SELECT max(meas_time) station_id FROM measurements WHERE meas_time <= 'time_to_query' GROUP BY station_id

Group By with MAX(TIMESTAMP) [duplicate]

给你一囗甜甜゛ 提交于 2019-12-12 01:54:57
问题 This question already has answers here : GROUP BY with MAX(DATE) [duplicate] (6 answers) Closed 5 years ago . I have a table showing letter id with year and created timestamp.I have to list the latest timestamp for every year. For example: This is my DATA. Letter ID YEAR TIMESTAMP 1411 2013 17-NOV-14 09.18.01.000000000 AM 1412 2013 16-NOV-14 09.18.01.000000000 AM 1413 2013 15-NOV-14 09.18.01.000000000 AM 1414 2013 14-NOV-14 09.18.01.000000000 AM 1415 2013 13-NOV-14 09.18.01.000000000 AM 1416

MySQL - Order by values in separate table

这一生的挚爱 提交于 2019-12-12 00:25:00
问题 I have two MySQL tables. Table1: id name otherid Table2: id otherid time In each table, the "id" is a unique identifier for every row (and is also the primary key) the "otherids" correspond between tables. There may be several (or 0) rows in table2 that have an otherid that corresponds to an otherid in table1. Table For example, Table 1: id name otherid 1 bob 24 2 joe 326 Table 2: id otherid time 4 326 2014-03-12 023:38:59 5 24 2013-02-11 22:05:21 6 24 2013-02-10 13:27:58 I am looking to

How to get latest two rows with certain value by date in SQL

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 22:58:56
问题 My question is that I have certain table with some varchar2 values and insert date. What I want to do is to get latest two such entries grouped by this varchar2 value Is it possible to include some top(2) instead of max in Oracle group by ? 回答1: EDIT Updated to not count duplicate date value for the same varchar2 . Replaced RANK() with DENSE_RANK() such that it assigns consecutive ranks, then used distinct to eliminate the duplicates. You can use DENSE_RANK() SELECT DISTINCT TXT, ENTRY_DATE

Ruby On Rails retrieve mysql database data 'a' distinct 'b'

孤街浪徒 提交于 2019-12-11 22:13:30
问题 My database have a device reading list, columns are id ,device_id ,device_reading ,update_time How can I select all latest reading for each device? that means,I need sort data according to update_time first then I need to filter database use some unique method(unique device_id) then the I can retrieve device_reading from rows filtered just now. That is to say, retrieve column a from table where b is distinct how can I achieve this? in ruby on rails thanks in advance 回答1: Supposing that model

How to get the latest 2 rows ( PostgreSQL )

限于喜欢 提交于 2019-12-11 18:08:45
问题 I need your help to modify a query to accomplish the requirement. According to the GATHER_TIME, I want to show the VALUE of the NAMEs. The query runs every 1 minute, and it should get 2 rows all the time. In case, there is no data at that time, it should get the past( 1 minute earlier ) data for the Name. Here, specific requirement is that I should not consider seconds value in Timestamp value(GATHER_TIME). I tried to make a query like below. It only gets the latest data. Can you help me out?

SQL - 2nd highest record in table

可紊 提交于 2019-12-11 17:23:35
问题 SELECT MAX(Score) FROM Students WHERE Score < (SELECT MAX(Score) FROM Students); the above query works perfectly and fetches the record that has 2nd highest score, whereas the query mentioned below does not fetch anything SELECT * FROM Students WHERE Score < (SELECT MAX(Score) FROM Students); here Students is the table from which I want to fetch all the details of that record which has 2nd highest score in the entire table. I want that 2nd query should get executed, thanks in advance for