Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Python :: latex new command with arguments 
Python :: do function for each 10sec with pyside2 
Python :: NxN Array 
Python :: line of best fit in linear regression 
Python :: sample mapping in pandas 
Python :: how to hide a specific file in python 
Python :: Python Windows Toggle Caps_Lock 
Python :: pypy tinytag 
Python :: parsing date columns when reading csv 
Python :: stripe white space django template 
Python :: added variable plot python 
Python :: How to make colors.winapp in WindowsAP 
Python :: auto clicker 
Python :: sample k-means clustering 
Python :: python SynC 
Python :: function for getting the basic statistic of our Dataframe in one go 
Python :: auto indent python code 
Python :: Filling or replacing the missing values with mode 
Python :: groupby and assign number to each group pandas 
Python :: iterate over batch of dict keys at once python 
Python :: python setup specify c++ version 
Python :: python pyhue 
Python :: trends in yearly data python 
Python :: create a python file and import it as library in other file 
Python :: adding attributes and metadata to a dataset using xarray 
Python :: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly 
Python :: how to use the dot lower function 
Python :: discord.py delete own message 
Python :: add up all the numbers in each row and output that number output the grand total of all rows 
Python :: one small letter three big bodyguard 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =