greatest-n-per-group

Proper way to access latest row for each individual identifier?

可紊 提交于 2020-01-11 10:45:12
问题 I have a table core_message in Postgres, with millions of rows that looks like this (simplified): ┌────────────────┬──────────────────────────┬─────────────────┬───────────┬──────────────────────────────────────────┐ │ Colonne │ Type │ Collationnement │ NULL-able │ Par défaut │ ├────────────────┼──────────────────────────┼─────────────────┼───────────┼──────────────────────────────────────────┤ │ id │ integer │ │ not null │ nextval('core_message_id_seq'::regclass) │ │ mmsi │ integer │ │ not

Using Max() function to select group values

半城伤御伤魂 提交于 2020-01-09 12:05:08
问题 I've got a table like this: SKU ITEM VALUE 1503796 1851920 0,9770637 1503796 1636691 0,9747891 1503796 1503781 0,9741025 1503796 3205763 0,9741025 1503801 1999745 0,9776622 1503801 1999723 0,9718825 1503801 3651241 0,9348839 1503801 1773569 0,9331309 1503811 1439825 0,97053134 1503811 1636684 0,96297866 1503811 1636671 0,96003973 1503811 1600553 0,9535771 1503818 1636708 0,9440251 1503818 1636709 0,9440251 1503818 1779789 0,9423958 1503818 3322310 0,9369579 I need to get output like this

Using Max() function to select group values

余生长醉 提交于 2020-01-09 12:02:10
问题 I've got a table like this: SKU ITEM VALUE 1503796 1851920 0,9770637 1503796 1636691 0,9747891 1503796 1503781 0,9741025 1503796 3205763 0,9741025 1503801 1999745 0,9776622 1503801 1999723 0,9718825 1503801 3651241 0,9348839 1503801 1773569 0,9331309 1503811 1439825 0,97053134 1503811 1636684 0,96297866 1503811 1636671 0,96003973 1503811 1600553 0,9535771 1503818 1636708 0,9440251 1503818 1636709 0,9440251 1503818 1779789 0,9423958 1503818 3322310 0,9369579 I need to get output like this

How to group by 2 fields and order by date at the same time?

那年仲夏 提交于 2020-01-06 14:22:53
问题 I have 3 fields in an employees table: personal_id , personal_id_type , and date_registered . I need to find all the rows, one row for each personal_id + personal_id_type (here is where I think I can use a GROUP), but since there may be many entries for the same personal_id + personal_id_type (because employees can quit and get hired again later), I need to fetch only the newest entry ( order by date_registered DESC ). Is this conceptually correct? What would be the syntax for grouping by 2

How to group by 2 fields and order by date at the same time?

雨燕双飞 提交于 2020-01-06 14:22:47
问题 I have 3 fields in an employees table: personal_id , personal_id_type , and date_registered . I need to find all the rows, one row for each personal_id + personal_id_type (here is where I think I can use a GROUP), but since there may be many entries for the same personal_id + personal_id_type (because employees can quit and get hired again later), I need to fetch only the newest entry ( order by date_registered DESC ). Is this conceptually correct? What would be the syntax for grouping by 2

SQLAlchemy ORM: Sum of products

坚强是说给别人听的谎言 提交于 2020-01-06 08:56:43
问题 Let's say I have a ROOMS table with width and length columns, and a corresponding SQLAlchemy model. Is there a clean and efficient way to get a total area of all rooms, i.e. sum(length x width)? It's easy enough to do by looping at the client but it must surely be more efficient if it can be fetched by a query at the server. Edit: I thought I was being helpful by reducing the problem to a simple, clean example but I now realise I was just shooting myself in the foot because my difficulties

Retrive row only if the column 'date' with the latest value have an another column not NULL

a 夏天 提交于 2020-01-06 06:58:08
问题 Let's say I have this table id | value | date | result ----+-------+------------+-------- 1 | 1 | 2017-01-01 | NULL 2 | 1 | 2017-01-02 | NULL 3 | 2 | 2017-01-03 | NULL 4 | 1 | 2017-01-04 | NULL 5 | 2 | 2017-01-05 | NULL 6 | 3 | 2017-01-06 | NULL 7 | 1 | 2017-01-07 | NULL My goal is to retrieve every "value" for which the last "date" has a "result" equal to NULL. For instance, with the previous table, my result should look like this: id | value | date | result ----+-------+------------+-------

specific row number with all the fields

妖精的绣舞 提交于 2020-01-06 05:27:09
问题 I have HR oracle schema and in that, I want to display all the fields of employees who have a 3rd highest salary. Here is what I have written so far but it does not return anything. select * from ( select * from HR.EMPLOYEES order by HR.EMPLOYEES.SALARY DESC) where rownum=3 I want to get this result without using any functions like dense_rank() etc. Want to keep the query simple. But if someone can use analytic function and can explain its working then I can get the idea of its use so far I

Select n rows per group in mysql

こ雲淡風輕ζ 提交于 2020-01-06 04:53:20
问题 I've read this article http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ which solves the problem I have, however I do not quite understand the solution. Here's the table (from the article). +--------+------------+-------+ | type | variety | price | +--------+------------+-------+ | apple | gala | 2.79 | | apple | fuji | 0.24 | | apple | limbertwig | 2.87 | | orange | valencia | 3.59 | | orange | navel | 9.36 | | pear | bradford | 6.05 | | pear |

Aggregation function to get the difference or ratio of two rows in order

和自甴很熟 提交于 2020-01-06 03:40:08
问题 I have a table full of prices, items, and dates. An example of this is: AA, 1/2/3024, 1.22 AA, 1/3/3024, 1.23 BB, 1/2/3024, 4.22 BB, 1/3/3024, 4.23 Within the data there are only two rows per price, and they are ordered by date. How would I condense this data set into a single product row showing the difference from the last price to the previous? [Also this applies to a ratio, so AA would produce 1.23/1.22]. The result should look like AA, todays price-yesterdays price Despite being a sum