当我运行以下查询时,出现上述错误。我意识到这是因为子查询返回超过 1 个结果,但不知道如何修复它。
update a
set a.covtypeplus = (SELECT distinct
REPLACE(REPLACE(ISNULL([BI],'x') + '+' + ISNULL([PD],'x') + '+' +
ISNULL([COM],'x') + '+' + ISNULL([COMGL],'x') + '+' + ISNULL([COL],'x') + '+' + ISNULL([COLDW],'x') + '+' +
ISNULL([MED],'x') + '+' + ISNULL([PIP],'x') + '+' + ISNULL([PIPNI],'x') + '+' + ISNULL([PIPNR],'x') + '+' +
ISNULL([UM],'x') + '+' + ISNULL([UMBI],'x') + '+' + ISNULL([UMBA],'x') + '+' + ISNULL([UMS],'x') + '+' +
ISNULL([UMPD],'x') + '+' + ISNULL([UMPA],'x') + '+' +
ISNULL([UIM],'x') + '+'+ ISNULL([UIMBI],'x') + '+' + ISNULL([UIMC],'x') + '+' + ISNULL([UIS],'x') + '+' +
ISNULL([UIMPD],'x') + '+' + ISNULL([MEX],'x') + '+' +
ISNULL([ACC],'x') + '+' + ISNULL([CPI],'x') + '+' + ISNULL([INC],'x') + '+' + ISNULL([FUN],'x') + '+' + ISNULL([XMD],'x')
, 'x+', ''), '+x', '') AS CovTypePlus
FROM (SELECT distinct POLICYID, VERSION, COVCODE FROM Staging.Coverage) p LEFT JOIN (
SELECT *
FROM (
SELECT POLICYID, VERSION, COVCODE
FROM Staging.Coverage
WHERE SUBSTRING(COVCODE, 1, 2) IN ('BI','PD','CO','ME','PI','UM','UI','AC','CP','IN','FU','XM','LD','RA','RE','RO','SP','TO','SG','WM')) p
PIVOT (MAX(COVCODE) FOR COVCODE IN (
[BI], [PD], [COM], [COMGL], [COL], [COLDW],
[MED], [PIP], [PIPNI], [PIPNR],
[UM], [UMBI], [UMBA], [UMS], [UMPD], [UMPA],
[UIM], [UIMBI], [UIMC], [UIS], [UIMPD],
[ACC], [CPI], [INC], [FUN], [XMD], [LD], [MEX], [RA], [REN], [ROADS], [SPE], [TOW], [SGC], [WMAR]
)) AS pvt) vc ON p.POLICYID = vc.POLICYID AND p.VERSION = vc.VERSION)
from results_vehicle a
left join staging.coverage c on a.polnum =c.policyid and a.polver = c.version and a.covcode = c.COVCODE
当我运行我创建的以下原始查询(不是更新)时,它工作正常,所以它一定是我在更新语法中做错了:
SELECT DISTINCT p.POLICYID AS PolNum, p.VERSION As PolVer,
REPLACE(REPLACE(ISNULL([BI],'x') + '+' + ISNULL([PD],'x') + '+' +
ISNULL([COM],'x') + '+' + ISNULL([COMGL],'x') + '+' + ISNULL([COL],'x') + '+' + ISNULL([COLDW],'x') + '+' +
ISNULL([MED],'x') + '+' + ISNULL([PIP],'x') + '+' + ISNULL([PIPNI],'x') + '+' + ISNULL([PIPNR],'x') + '+' +
ISNULL([UM],'x') + '+' + ISNULL([UMBI],'x') + '+' + ISNULL([UMBA],'x') + '+' + ISNULL([UMS],'x') + '+' +
ISNULL([UMPD],'x') + '+' + ISNULL([UMPA],'x') + '+' +
ISNULL([UIM],'x') + '+'+ ISNULL([UIMBI],'x') + '+' + ISNULL([UIMC],'x') + '+' + ISNULL([UIS],'x') + '+' +
ISNULL([UIMPD],'x') + '+' + ISNULL([MEX],'x') + '+' +
ISNULL([ACC],'x') + '+' + ISNULL([CPI],'x') + '+' + ISNULL([INC],'x') + '+' + ISNULL([FUN],'x') + '+' + ISNULL([XMD],'x')
, 'x+', ''), '+x', '') AS CovTypePlus
FROM (SELECT DISTINCT POLICYID, VERSION, COVCODE FROM Staging.Coverage) p LEFT JOIN (
SELECT *
FROM (
SELECT POLICYID, VERSION, COVCODE
FROM Staging.Coverage
WHERE SUBSTRING(COVCODE, 1, 2) IN
('BI','PD','CO','ME','PI','UM','UI','AC','CP','IN','FU','XM','LD','RA','RE','RO','SP','TO','SG','WM')) p
PIVOT (MAX(COVCODE) FOR COVCODE IN (
[BI], [PD], [COM], [COMGL], [COL], [COLDW],
[MED], [PIP], [PIPNI], [PIPNR],
[UM], [UMBI], [UMBA], [UMS], [UMPD], [UMPA],
[UIM], [UIMBI], [UIMC], [UIS], [UIMPD],
[ACC], [CPI], [INC], [FUN], [XMD], [LD], [MEX], [RA], [REN], [ROADS], [SPE], [TOW], [SGC], [WMAR]
)) AS pvt) vc ON p.POLICYID = vc.POLICYID AND p.VERSION = vc.VERSION
非常感谢任何帮助,我尝试过使用查询并产生相同的结果。我还尝试添加 SELECT TOP 1 并为所有策略获得相同的结果。