Hi All,
Here is my sample script :
TMP1 have 2 rows with identical values of the field used by Partition , thus TMP2 is picking randomly when doing RN=1.
Can this be fix ?
WITH TMP1 AS (
SELECT
distinct [HERO ID],
[Service Unit Name],
[Location Facility ID],
[Health Service],
Department,
,Division, Specialty,
[Effective To],
[Effective from],
[Secure Org Flag],
, ISNULL([Service Unit Hierarchy],99) AS [Service Unit Hierarchy]
FROM VIEWMAPPING
), TMP2 AS (
SELECT *, RN=ROW_NUMBER() OVER(PARTITION BY [HERO ID] ORDER BY [Service Unit Hierarchy] ASC, [Effective To] Desc, [Effective From] Desc, [Location Facility ID] ) FROM TMP1
)
SELECT [HERO ID],
[Service Unit Name],
[Location Facility ID],
Department,
Division, Specialty,
[Effective To],
[Effective from],
[Secure Org Flag]
FROM TMP2 WHERE RN=1
Thanks