Row_number over partition and find the max rn value

Nicolaesse

I started from the following table:

+---+-----+---------+----------+----------+-------------+
| 1 | ID  | OrderNO | PartCode | Quantity | DateEntered |
| 2 | 417 | 2144    | 44917    | 100      | 40773       |
| 3 | 418 | 7235    | 11762    | 5        | 40773       |
| 4 | 419 | 9999    | 60657    | 100      | 40773       |
| 5 | 420 | 9999    | 60657    | 90       | 40774       |
+---+-----+---------+----------+----------+-------------+

to this:

+---+---------+----------+----------+-------------+----+
| 1 | OrderNO | PartCode | Quantity | DateEntered | rn |
| 2 | 2144    | 44917    | 100      | 40773       | 1  |
| 3 | 7235    | 11762    | 5        | 40773       | 1  |
| 4 | 9999    | 60657    | 100      | 40773       | 1  |
| 5 | 9999    | 60657    | 90       | 40774       | 2  |
+---+---------+----------+----------+-------------+----+

using the query fo this answer.

select OrderNO,PartCode,Quantity,row_number() over(partition by OrderNO order by DateEntered desc) as rn
from YourTable) as T

Now, I am trying to add a field "rn_max", shich is the max "rn" of each OrderNO and get the following result:

+---+---------+----------+----------+-------------+----+--------+
| 1 | OrderNO | PartCode | Quantity | DateEntered | rn | rn_max |
| 2 | 2144    | 44917    | 100      | 40773       | 1  | 1      |
| 3 | 7235    | 11762    | 5        | 40773       | 1  | 1      |
| 4 | 9999    | 60657    | 100      | 40773       | 1  | 2      |
| 5 | 9999    | 60657    | 90       | 40774       | 2  | 2      |
+---+---------+----------+----------+-------------+----+--------+

I could get this result calculating the max of each OrderNO with the query

SELECT OrderNO,MAX(rn) AS 'rn_max'
FROM (
select OrderNO,PartCode,Quantity,row_number() over(partition by OrderNO order by DateEntered desc) as rn
from YourTable
) T
Group by OrderNO

and then use a left join from YourTable. Is there a shortest way to count the rows with the same OderNo? I've tried to add row_number() over(partition by OrderNO) as rn_max at the query but I need to write an order by clause.

Vamsi Prabhala

Use max window function.

SELECT T.*,MAX(rn) OVER(PARTITION BY OrderNo) AS rn_max
FROM (
select OrderNO,PartCode,Quantity,row_number() over(partition by OrderNO order by DateEntered desc) as rn
from YourTable
) T

Edit: An easier option is to use count as suggested by @Jason A. Long in the comments.

select OrderNO
       ,PartCode
       ,Quantity
       ,row_number() over(partition by OrderNO order by DateEntered desc) as rn
       ,count(*) over(partition by OrderNO) as maxrn
from YourTable

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

MySQL Convert ROW_NUMBER()OVER PARTITION

分類Dev

Row_Number over(partition by ...)すべての列

分類Dev

SQLエラー:ROW_NUMBER()OVER(PARTITION

分類Dev

row_number() over(partition by col) equivalent in Mysql 5.7 version

分類Dev

Entity FrameworkのROW_NUMBER OVER PARTITION BY ORDER BY

分類Dev

ROW_NUMBER (), PARTITION_BY, TOP 2 MAX se MAX 1ª e última posição

分類Dev

mysql 5.7バージョンで同等のrow_number()over(partition by col)

分類Dev

Where句でROW_NUMBER()OVER(PARTITION BY ...)を指定してstmtを選択します

分類Dev

選択されていない列で(ROW_NUMBER()OVER PARTITION BY)を注文する方法

分類Dev

Oracle SQLのMAX()OVER PARTITION BY

分類Dev

max、over partition by、oracle、take max rownum record

分類Dev

ROW_NUMBER()OVER(PARTITION BYによりmysql5.7で構文エラーが発生する

分類Dev

パンダ:上位N行、グループごとの上位N行、ROW_NUMBER OVER(PARTITION BY ... ORDER BY ...)と同等

分類Dev

row_number()over(partition by col)がMysql5.7バージョンで機能しない

分類Dev

Pandas: find column name and value with max (and second max) value for each row

分類Dev

Max()Over Partition By、日付条件付き

分類Dev

Row_number() and partition by statement of sql is not working in java class

分類Dev

Find max value in sql table

分類Dev

PostgreSQL:row_number:エラー:列 "rn"が存在しません

分類Dev

ROW_NUMBER()OVER()、H2でのorder by

分類Dev

Excel Find Value next to max value

分類Dev

Select row with least value in multiple columns without ROW_NUMBER

分類Dev

SQL Server 2008でのCount(Distinct([value))OVER(Partition by)

分類Dev

FIRST_VALUE OVER PARTITION BY ORDERBYのエラー

分類Dev

How to find the next Max Value in an array

分類Dev

Find max value(s) of certain key

分類Dev

find max value of a natural join table

分類Dev

Find min and max value for each year with date

分類Dev

Find max value within a data frame interval

Related 関連記事

  1. 1

    MySQL Convert ROW_NUMBER()OVER PARTITION

  2. 2

    Row_Number over(partition by ...)すべての列

  3. 3

    SQLエラー:ROW_NUMBER()OVER(PARTITION

  4. 4

    row_number() over(partition by col) equivalent in Mysql 5.7 version

  5. 5

    Entity FrameworkのROW_NUMBER OVER PARTITION BY ORDER BY

  6. 6

    ROW_NUMBER (), PARTITION_BY, TOP 2 MAX se MAX 1ª e última posição

  7. 7

    mysql 5.7バージョンで同等のrow_number()over(partition by col)

  8. 8

    Where句でROW_NUMBER()OVER(PARTITION BY ...)を指定してstmtを選択します

  9. 9

    選択されていない列で(ROW_NUMBER()OVER PARTITION BY)を注文する方法

  10. 10

    Oracle SQLのMAX()OVER PARTITION BY

  11. 11

    max、over partition by、oracle、take max rownum record

  12. 12

    ROW_NUMBER()OVER(PARTITION BYによりmysql5.7で構文エラーが発生する

  13. 13

    パンダ:上位N行、グループごとの上位N行、ROW_NUMBER OVER(PARTITION BY ... ORDER BY ...)と同等

  14. 14

    row_number()over(partition by col)がMysql5.7バージョンで機能しない

  15. 15

    Pandas: find column name and value with max (and second max) value for each row

  16. 16

    Max()Over Partition By、日付条件付き

  17. 17

    Row_number() and partition by statement of sql is not working in java class

  18. 18

    Find max value in sql table

  19. 19

    PostgreSQL:row_number:エラー:列 "rn"が存在しません

  20. 20

    ROW_NUMBER()OVER()、H2でのorder by

  21. 21

    Excel Find Value next to max value

  22. 22

    Select row with least value in multiple columns without ROW_NUMBER

  23. 23

    SQL Server 2008でのCount(Distinct([value))OVER(Partition by)

  24. 24

    FIRST_VALUE OVER PARTITION BY ORDERBYのエラー

  25. 25

    How to find the next Max Value in an array

  26. 26

    Find max value(s) of certain key

  27. 27

    find max value of a natural join table

  28. 28

    Find min and max value for each year with date

  29. 29

    Find max value within a data frame interval

ホットタグ

アーカイブ