I am using this code: (from this question: How to get the last record per group in SQL substituting my own columns)
WITH e AS
(
SELECT *,
ROW_NUMBER() OVER
(
PARTITION BY ApplicationId
ORDER BY theDate DESC
) AS Recency
FROM [Event]
)
SELECT *
FROM e
WHERE Recency = 1
Is it possible to 'partition' only if two fields are the same? For example I have data like this:
ID Name theDate
123 John 01/01/2012
123 John 01/02/2012
123 Doe 01/01/2012
456 Smith 02/04/2012
789 Smith 02/01/2012
789 Smith 02/09/2012
789 Roger 02/08/2012
From that data I'd want to return:
ID Name theDate
123 John 01/02/2012
123 Doe 01/01/2012
456 Smith 02/04/2012
789 Smith 02/09/2012
789 Roger 02/08/2012
Thanks for any help.
Thomas
ROW_NUMBER() Function without Partition By clausePartition by clause is an optional part of Row_Number function and if you don't use it all the records of the result-set will be considered as a part of single record group or a single partition and then ranking functions are applied.
The ROW_NUMBER() function uses the OVER and PARTITION BY clause and sorts results in ascending or descending order. It starts ranking rows from 1 per the sorting order.
The row_number() window function can be used without order by in over to arbitrarily assign a unique value to each row.
Because the ROW_NUMBER function requires an ORDER BY clause, the ROW_NUMBER function specifies ORDER BY (SELECT 1) to return the rows in the order in which they are stored in the specified table and sequentially number them, starting from 1.
You can have several columns separated by a comma
WITH e AS
(
SELECT *,
ROW_NUMBER() OVER
(
PARTITION BY ApplicationId , Name
ORDER BY theDate DESC
) AS Recency
FROM [Event]
)
SELECT *
FROM e
WHERE Recency = 1
I've found it the answer here: Table partitioning using 2 columns
You can only partition on 1 column, however that column can be generated to make a 'multiple partition' like so:
WITH e AS
(
SELECT *,
ROW_NUMBER() OVER
(
PARTITION BY CONVERT(VARCHAR(100),ApplicationId) + ' ' + Name
ORDER BY theDate DESC
) AS Recency
FROM [Event]
)
SELECT *
FROM e
WHERE Recency = 1
Adding the two columns together as one single string ensures it will only partition if both columns are identical.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With