子查询返回的值超过1。子查询遵循=,!=,<时不允许这样做,

时间:2013-05-15 18:47:46

标签: sql sql-server

当我运行以下查询时,我收到上述错误。我意识到这是因为子查询返回的结果超过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,并为所有策略获得相同的结果。

2 个答案:

答案 0 :(得分:0)

你在做:

update a
set a.covtypeplus = (SELECT distinct . . .

这表明您期望来自该子查询的多个结果。不允许。

也许这会解决它,但它可能不是你想要的:

update a
set a.covtypeplus = (SELECT top 1 . . .

答案 1 :(得分:0)

这部分似乎搞砸了你:

from results_vehicle a
    left join staging.coverage c on a.polnum =c.policyid
       and a.polver = c.version and a.covcode = c.COVCODE

这必须为a.covcode的某些值生成多个结果。