根据最新日期获取记录
Get records based on latest date
在下面的查询中,我只需要获取一条记录,即最新的 phDate。
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
--select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId
CTE 的预期输出 只为每个 ppId 获取最新的 phDate,如果超过 1 条记录则忽略其他记录,然后更新它。
使用 partition by 子句可以获得预期的输出。尝试以下查询:
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate , ROW_NUMBER() OVER(PARTITION BY S.ppId ORDER BY phDate DESC) AS NUM
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P
ON T.ppId = P.PatientId AND T.NUM=1
在 where 子句中添加 phDate = max phDate 条件:
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
SELECT *
FROM TEMP_PAT_PIP_AddedBy
where phDate = (select max(phDate) from TEMP_PAT_PIP_AddedBy
where ppId = TEMP_PAT_PIP_AddedBy.ppId)
ORDER BY ppId
,phDate DESC
编辑版,phDate是P栏吗? (如果不是,请编辑我的答案。)
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
AND phDate = (select max(phDate) from P
where ppId = P.ppId)
)
--select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId
斯文斯克?
在下面的查询中,我只需要获取一条记录,即最新的 phDate。
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
--select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId
CTE 的预期输出 只为每个 ppId 获取最新的 phDate,如果超过 1 条记录则忽略其他记录,然后更新它。
使用 partition by 子句可以获得预期的输出。尝试以下查询:
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate , ROW_NUMBER() OVER(PARTITION BY S.ppId ORDER BY phDate DESC) AS NUM
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P
ON T.ppId = P.PatientId AND T.NUM=1
在 where 子句中添加 phDate = max phDate 条件:
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
SELECT *
FROM TEMP_PAT_PIP_AddedBy
where phDate = (select max(phDate) from TEMP_PAT_PIP_AddedBy
where ppId = TEMP_PAT_PIP_AddedBy.ppId)
ORDER BY ppId
,phDate DESC
编辑版,phDate是P栏吗? (如果不是,请编辑我的答案。)
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
AND phDate = (select max(phDate) from P
where ppId = P.ppId)
)
--select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId
斯文斯克?