以下查询提取相同的数据,但有两个不同的PTACHI_PK。我只需要显示最高的PTACHI_PK。我不确定如何在SQL Server中实现这一点。
SELECT DISTINCT
PTACHI_PK,
ACT.TAX_PAYER_ID AS 'AP_OWENER_ID',
IDM.ID_PK AS 'NCPTS_OWNER_ID',
'' as REL_PK,
ADDR.ADDRESS_PK,
FIRST_NAME, MIDDLE_NAME, LAST_NAME,
BUSINESS_NAME,
ADDR1, ADDR2, ADDR3,
IN_CARE_OF,
CITY,
RIGHT(STATE,2) as 'STATE',
POSTAL_CODE AS 'ZIP',
POSTAL_CODE_EXT AS 'ZIP4',
HIS.FIELD_CHANGED,
HIS.FIELD_OLD_VALUE,
HIS.FIELD_NEW_VALUE,
CONVERT(VARCHAR, MODIFIED_TS, 22) AS 'DATE_CHANGED'
FROM
OWNIDM_ID_MASTER IDM
JOIN
OWNREL_OWNER_RELATIONSHIP REL ON IDM.ID_PK = REL.IDM_ID_PK
JOIN
OWNACT_ACCOUNT ACT ON REL.ACT_ACCOUNT_PK = ACT.ACCOUNT_PK
JOIN
COMADD_COMMON_ADDRESS ADDR ON REL.ADD_ADDRESS_PK = ADDR.ADDRESS_PK
JOIN
COMNAM_COMMON_NAME NAME ON REL.NAM_NAME_PK = NAME.NAME_PK
JOIN
PTACHI_CHANGE_HISTORY HIS ON IDM.ID_PK = HIS.PARENT_PK
WHERE
ACT.TAX_PAYER_ID IS NOT NULL
AND ACT.TAX_PAYER_ID <> ''
AND ISDEFAULT_ADDRESS = 'Y'
AND ADDR.SOURCE_TYPE <> 'ADSRDMV'
AND HIS.PARENT_TYPE = 'COMADD_COMMON_ADDRESS'
AND HIS.FIELD_CHANGED = 'OWNER_ADDRESS_UPDATED'
AND MODIFIED_TS >= DATEADD(day, DATEDIFF(day,0,GETDATE())-1,0)
AND MODIFIED_TS < DATEADD(day, DATEDIFF(day,0,GETDATE()),0)
AND ID_PK = 432082
GROUP BY
ACT.TAX_PAYER_ID, IDM.ID_PK, ADDR.ADDRESS_PK,
FIRST_NAME, MIDDLE_NAME, LAST_NAME,
BUSINESS_NAME, ADDR1, ADDR2, ADDR3,
IN_CARE_OF, CITY, STATE,
POSTAL_CODE, POSTAL_CODE_EXT,
HIS.FIELD_CHANGED, HIS.FIELD_OLD_VALUE,
HIS.FIELD_NEW_VALUE, MODIFIED_TS, PTACHI_PK
ORDER BY
PTACHI_PK DESC
我正在使用SQL Server。附上一些结果样本。
答案 0 :(得分:0)
SELECT TOP 1
.....
ORDER BY PTACHI_PK DESC
查看它是否有效并在合理的时间内运行。
答案 1 :(得分:0)
您应该能够使用相同的连接和过滤器来确定最大值。例如:
SELECT MAX(PTACHI_PK)
FROM OWNIDM_ID_MASTER IDM
JOIN OWNREL_OWNER_RELATIONSHIP REL ON IDM.ID_PK = REL.IDM_ID_PK
JOIN OWNACT_ACCOUNT ACT ON REL.ACT_ACCOUNT_PK = ACT.ACCOUNT_PK
JOIN COMADD_COMMON_ADDRESS ADDR ON REL.ADD_ADDRESS_PK = ADDR.ADDRESS_PK
JOIN COMNAM_COMMON_NAME NAME ON REL.NAM_NAME_PK = NAME.NAME_PK
JOIN PTACHI_CHANGE_HISTORY HIS ON IDM.ID_PK = HIS.PARENT_PK
WHERE
ACT.TAX_PAYER_ID IS NOT NULL
AND ACT.TAX_PAYER_ID <> ''
AND ISDEFAULT_ADDRESS = 'Y'
AND ADDR.SOURCE_TYPE <> 'ADSRDMV'
AND HIS.PARENT_TYPE = 'COMADD_COMMON_ADDRESS'
AND HIS.FIELD_CHANGED = 'OWNER_ADDRESS_UPDATED'
AND MODIFIED_TS >= DATEADD(day, DATEDIFF(day,0,GETDATE())-1,0)
AND MODIFIED_TS < DATEADD(day, DATEDIFF(day,0,GETDATE()),0)
AND ID_PK = 432082
知道如何获得最大值,您可以将其作为原始算法的Where
中的过滤器:
WHERE
ACT.TAX_PAYER_ID IS NOT NULL
AND ACT.TAX_PAYER_ID <> ''
AND ISDEFAULT_ADDRESS = 'Y'
AND ADDR.SOURCE_TYPE <> 'ADSRDMV'
AND HIS.PARENT_TYPE = 'COMADD_COMMON_ADDRESS'
AND HIS.FIELD_CHANGED = 'OWNER_ADDRESS_UPDATED'
AND MODIFIED_TS >= DATEADD(day, DATEDIFF(day,0,GETDATE())-1,0)
AND MODIFIED_TS < DATEADD(day, DATEDIFF(day,0,GETDATE()),0)
AND ID_PK = 432082
--using the code for MAX(PTACHI_PK) to filter PTACHI_PK:
AND PTACHI_PK =
(SELECT MAX(PTACHI_PK)
FROM OWNIDM_ID_MASTER IDM
JOIN OWNREL_OWNER_RELATIONSHIP REL ON IDM.ID_PK = REL.IDM_ID_PK
JOIN OWNACT_ACCOUNT ACT ON REL.ACT_ACCOUNT_PK = ACT.ACCOUNT_PK
JOIN COMADD_COMMON_ADDRESS ADDR ON REL.ADD_ADDRESS_PK = ADDR.ADDRESS_PK
JOIN COMNAM_COMMON_NAME NAME ON REL.NAM_NAME_PK = NAME.NAME_PK
JOIN PTACHI_CHANGE_HISTORY HIS ON IDM.ID_PK = HIS.PARENT_PK
WHERE
ACT.TAX_PAYER_ID IS NOT NULL
AND ACT.TAX_PAYER_ID <> ''
AND ISDEFAULT_ADDRESS = 'Y'
AND ADDR.SOURCE_TYPE <> 'ADSRDMV'
AND HIS.PARENT_TYPE = 'COMADD_COMMON_ADDRESS'
AND HIS.FIELD_CHANGED = 'OWNER_ADDRESS_UPDATED'
AND MODIFIED_TS >= DATEADD(day, DATEDIFF(day,0,GETDATE())-1,0)
AND MODIFIED_TS < DATEADD(day, DATEDIFF(day,0,GETDATE()),0)
AND ID_PK = 432082
)
另外,我会接受Aaron Bertrand的建议而不是distinct
加group by
,因为它不必要地删除重复次数两次。