June 29, 2021 at 10:07 am
I have written the query using ranking function. I tried to write the same query using sub query no clue. Can any body help
The query below as follows
WITH HighRecords AS
(
SELECT
BusinessEntityID,
FirstName,
MiddleName,
LastName,
TotalDue,
ROW_NUMBER() OVER(PARTITION BY BusinessEntityId ORDER BY TotalDue ASC) AS RN
FROM Person.Person Pp
INNER JOIN Sales.Customer Sc
ON Sc.PersonID=Pp.BusinessEntityID
INNER JOIN Sales.SalesOrderHeader Ss
ON Sc.CustomerID=Ss.CustomerID
)
SELECT BusinessEntityID,
FirstName,
MiddleName,
LastName,
TotalDue FROM HighRecords
ORDER BY BusinessEntityID ASC,TotalDue ASC
June 29, 2021 at 10:21 am
The query you have provided contains both a subquery and a ranking function, so I don't understand what you are asking for.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
June 29, 2021 at 10:25 am
SELECT BusinessEntityID,
FirstName,
MiddleName,
LastName,
TotalDue
FROM (SELECT BusinessEntityID,
FirstName,
MiddleName,
LastName,
TotalDue,
ROW_NUMBER() OVER(PARTITION BY BusinessEntityId ORDER BY TotalDue ASC) AS RN
FROM Person.Person Pp
INNER JOIN Sales.Customer Sc
ON Sc.PersonID = Pp.BusinessEntityID
INNER JOIN Sales.SalesOrderHeader Ss
ON Sc.CustomerID = Ss.CustomerID) AS HighRecords
ORDER BY BusinessEntityID ASC, TotalDue ASC
June 29, 2021 at 11:20 am
Can this be achieved without using ranking function
June 30, 2021 at 9:42 pm
What are you trying to do??? Get just the rows with the highest TotalDue for each BusinessEntityId ??? I so, just change the ASC in your ROW_NUMBER() to DESC and add a WHERE RN = 1 to your original query.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply