在查询结果中,我得到了特定策略中用于驾驶员的重复车辆,并且在我的初始查询中结果还不错。这是我的结果:
从上面的图片中,我只想显示POLICYHOLDER的车辆记录,并在同一策略中从ACTIVE记录中删除这些相同的车辆。这就是我希望的样子:
这是我的sql代码:
WITH CTE1 as
(
SELECT pr1.*
FROM #tempPIFRecords pr1
),
CTE2 as (
SELECT pavr.*
FROM #tempPIFAssocVehicleRecords pavr
)
SELECT CTE1.*, CTE2.Year, CTE2.Make, CTE2.Model, CTE2.[Effective Date], CTE2.[Expiration Date]
FROM CTE1
LEFT OUTER JOIN CTE2
ON CTE1.AgentNo = CTE2.AgentNo
AND CTE1.PolicyNo = CTE2.PolicyNo
ORDER BY CTE1.AgentNo asc, CTE1.PolicyNo asc;
任何帮助/指导将不胜感激。谢谢。
答案 0 :(得分:1)
应该可以:
WITH CTE1 as
(
SELECT pr1.*
FROM #tempPIFRecords pr1
),
CTE2 as (
SELECT pavr.*
FROM #tempPIFAssocVehicleRecords pavr
)
SELECT CTE1.*, ISNULL(CTE2.Year, '') AS Year, ISNULL(CTE2.Make, '') AS MAke, ISNULL(CTE2.Model, '') AS Model, ISNULL(CTE2.[Effective Date], '') AS [Effective Date], ISNULL(CTE2.[Expiration Date], '') AS [Expiration Date]
FROM CTE1
LEFT OUTER JOIN CTE2
ON CTE1.AgentNo = CTE2.AgentNo
AND CTE1.PolicyNo = CTE2.PolicyNo
AND CTE1.[DriverType] = 'PolicyHolder'
ORDER BY CTE1.AgentNo asc, CTE1.PolicyNo asc;