get first row for each group

stefan

I want to transform this data

account; docdate; docnum
17700; 9/11/2015; 1
17700; 9/12/2015; 2
70070; 9/1/2015; 4
70070; 9/2/2015; 6
70070; 9/3/2015; 9

into this

account; docdate; docnum
17700; 9/12/2015; 2
70070; 9/3/2015; 9

.. for each account I want to have one row with the most current (=max(docdate)) docdate. I already tried different approaches with cross apply and row_number but couldn't achieve the desired results

Giorgos Betsos

Use ROW_NUMBER:

SELCT account, docdate, docnum
FROM (
  SELECT account, docdate, docnum,
         ROW_NUMBER() OVER (PARTITION BY account 
                            ORDER BY docdate DESC) AS rn
  FROM mytable ) AS t
WHERE t.rn = 1 

PARTITION BY account clause creates slices of rows sharing the same account value. ORDER BY docdate DESC places the record having the maximum docdate value at the top of its related slice. Hence rn = 1 points to the record with the maximum docdate value within each account partition.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Pandas dataframe get first row of each group

From Dev

First row for each group

From Dev

How to get first row from each group from a table in sqlitedatabase?

From Java

Select first row in each GROUP BY group?

From Dev

Select first row in each GROUP BY group

From Dev

Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

From Dev

Group by and get the latest row from each group

From Java

How to select the first row of each group?

From Dev

Adding a rank to first row of each group

From Dev

KDB selecting first row from each group

From Dev

Laravel eloquent select first row of each group by

From Dev

Select first row in each contiguous run by group

From Dev

Select first row in each contiguous run by group

From Dev

pandas: how do I select first row in each GROUP BY group?

From Dev

SQL Server Group By Query Select first row each group

From Dev

Select first row in each group using group by in sqlite

From Java

Get top 1 row of each group

From Dev

Get each row's value and that of the group in dplyr

From Dev

Get each row's value and that of the group in dplyr

From Dev

Get a list of first record for each group

From Dev

Get the first occurence of the result in each specified group

From Dev

Get the first occurence of the result in each specified group

From Dev

Get First element by the recent date of each group

From Dev

Get first cell value of each row in table

From Dev

drop first and last row from within each group

From Dev

Select first row from group each by with count using Big Query

From Dev

Show only first row of each group excluding NULL

From Dev

Select first row from group each by with count using Big Query

From Dev

Dropping/removing last/first row in each group R

Related Related

  1. 1

    Pandas dataframe get first row of each group

  2. 2

    First row for each group

  3. 3

    How to get first row from each group from a table in sqlitedatabase?

  4. 4

    Select first row in each GROUP BY group?

  5. 5

    Select first row in each GROUP BY group

  6. 6

    Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

  7. 7

    Group by and get the latest row from each group

  8. 8

    How to select the first row of each group?

  9. 9

    Adding a rank to first row of each group

  10. 10

    KDB selecting first row from each group

  11. 11

    Laravel eloquent select first row of each group by

  12. 12

    Select first row in each contiguous run by group

  13. 13

    Select first row in each contiguous run by group

  14. 14

    pandas: how do I select first row in each GROUP BY group?

  15. 15

    SQL Server Group By Query Select first row each group

  16. 16

    Select first row in each group using group by in sqlite

  17. 17

    Get top 1 row of each group

  18. 18

    Get each row's value and that of the group in dplyr

  19. 19

    Get each row's value and that of the group in dplyr

  20. 20

    Get a list of first record for each group

  21. 21

    Get the first occurence of the result in each specified group

  22. 22

    Get the first occurence of the result in each specified group

  23. 23

    Get First element by the recent date of each group

  24. 24

    Get first cell value of each row in table

  25. 25

    drop first and last row from within each group

  26. 26

    Select first row from group each by with count using Big Query

  27. 27

    Show only first row of each group excluding NULL

  28. 28

    Select first row from group each by with count using Big Query

  29. 29

    Dropping/removing last/first row in each group R

HotTag

Archive