groupwise-maximum

LEFT JOIN only first row

荒凉一梦 提交于 2019-11-27 17:13:48
I read many threads about getting only the first row of a left join, but, for some reason, this does not work for me. Here is my structure (simplified of course) Feeds id | title | content ---------------------- 1 | Feed 1 | ... Artists artist_id | artist_name ----------------------- 1 | Artist 1 2 | Artist 2 feeds_artists rel_id | artist_id | feed_id ---------------------------- 1 | 1 | 1 2 | 2 | 1 ... Now i want to get the articles and join only the first Artist and I thought of something like this: SELECT * FROM feeds LEFT JOIN feeds_artists ON wp_feeds.id = ( SELECT feeds_artists.feed_id

Optimize groupwise maximum query

陌路散爱 提交于 2019-11-27 05:28:43
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 rows=200 width=4) (actual time=1439.203..1439.503 rows=1057 loops=1) -> HashAggregate (cost=30196.72.

Finding running maximum by group

不打扰是莪最后的温柔 提交于 2019-11-26 21:04:17
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, 3L, 4L, 5L)), .Names = c("var", "group","time"), class = "data.frame", row.names = c(NA, -10L))) #

LEFT JOIN only first row

南笙酒味 提交于 2019-11-26 18:54:48
问题 I read many threads about getting only the first row of a left join, but, for some reason, this does not work for me. Here is my structure (simplified of course) Feeds id | title | content ---------------------- 1 | Feed 1 | ... Artists artist_id | artist_name ----------------------- 1 | Artist 1 2 | Artist 2 feeds_artists rel_id | artist_id | feed_id ---------------------------- 1 | 1 | 1 2 | 2 | 1 ... Now i want to get the articles and join only the first Artist and I thought of something

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

♀尐吖头ヾ 提交于 2019-11-25 22:50:41
问题 This question already has an answer here: Retrieving the last record in each group - MySQL 25 answers I have this table for documents (simplified version here): +------+-------+--------------------------------------+ | id | rev | content | +------+-------+--------------------------------------+ | 1 | 1 | ... | | 2 | 1 | ... | | 1 | 2 | ... | | 1 | 3 | ... | +------+-------+--------------------------------------+ How do I select one row per id and only the greatest rev? With the above data,