Search
 
SCRIPT & CODE EXAMPLE
 

SQL

Get top 1 row of each group

;WITH cte AS
(
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY DocumentID ORDER BY DateCreated DESC) AS rn
   FROM DocumentStatusLogs
)
SELECT *
FROM cte
WHERE rn = 1
Comment

Get top 1 row of each group

|ID| DocumentID | Status | DateCreated |
| 2| 1          | S1     | 7/29/2011   |
| 3| 1          | S2     | 7/30/2011   |
| 6| 1          | S1     | 8/02/2011   |
| 1| 2          | S1     | 7/28/2011   |
| 4| 2          | S2     | 7/30/2011   |
| 5| 2          | S3     | 8/01/2011   |
| 6| 3          | S1     | 8/02/2011   |


;WITH cte AS
(
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY DocumentID ORDER BY DateCreated DESC) AS rn
   FROM DocumentStatusLogs
)
SELECT *
FROM cte
WHERE rn = 1


Comment

Top n rows of each group

df1 = df.sort_values('score',ascending = False).groupby('pidx').head(2)
print (df1)

    mainid pidx pidy  score
8        2    x    w     12
4        1    a    e      8
2        1    c    a      7
10       2    y    x      6
1        1    a    c      5
7        2    z    y      5
6        2    y    z      3
3        1    c    b      2
5        2    x    y      1
Comment

PREVIOUS NEXT
Code Example
Sql :: NLS_NCHAR_CHARACTERSET 
Sql :: column value should show as latest using sql query 
Sql :: oracle run_duration to number 
Sql :: mysql reset wp_options 
Sql :: how to remove quotes from a string in ssis load file 
Sql :: sql xampp gabungan nama awal dan akhir 
Sql :: postgres row expiration 
Sql :: how to input data as id in database sql c# 
Sql :: update all linkedserver tables with openquery on db 
Sql :: setval postgres example table id 
Sql :: soql queries for not contact related account records in salesforce 
Sql :: remove an object that is dependent on a column in sql 
Sql :: T-SQL look for records with a hex value in a text field 
Sql :: mysql set user password for a range of ips 
Sql :: delete hangfire retries list 
Sql :: Time difference in hh:mm:ss 
Sql :: postgresql GRANT role_2 TO role_1 
Sql :: okta postgresql 
Sql :: C# check if mysql query modified rows 
Sql :: kill slow queries mysql 
Sql :: oracle APEX elapsed time 
Sql :: SQLServerException.makeFromDatabaseError 
Sql :: mysql db dump restore max file size issue 
Sql :: subconjuntos SQL 
Sql :: select statement to print shortest name 
Sql :: postgres drop all tables owned by a user 
Sql :: mysql auto increment jumping 
Sql :: repeatable read trong sql server 
Sql :: How to group by week (7 days) in SQL Server 
Sql :: select 1 from orders 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =