根据该行中的值更新表中的每一行

时间:2015-04-24 02:57:01

标签: sql-server sql-server-2008 tsql

我正在尝试使用每行的值更新Declared表中的每一行。我不想写一个光标来做它我宁愿使用基于集合的操作,但我画了一个空白。谁能指出我正确的方向?

我目前获得的ValidContactPhoneCount列的结果是82013,它是表中所有帐户的所有有效数字的计数,而不是每个帐户的计数。我希望每行有0到5个数字。

DECLARE @Open TABLE
    (
    AccountID INT,
    AccountProcessID INT,
    ServicerID INT,
    ProcessTemplateID INT,
    ProcessStageID INT,
    CurrentStage VARCHAR(50),
    CurrentStageID INT,
    ValidContactPhoneCount INT
    )

INSERT INTO @Open

-- Identify accounts in open inventory for servicers with campaigns by template and stage

SELECT  ap.AccountID,
        ap.AccountProcessID,
        ap.ServicerID,
        ap.ProcessTemplateID,
        ap.ProcessStageID,
        ps.StageDesc,
        ap.ProcessStageID,
        NULL
FROM AccountProcess ap WITH (NOLOCK)
    JOIN ProcessStage ps WITH (NOLOCK)
        ON ps.ProcessStageID = ap.ProcessStageID
    JOIN ADSDialerAutomation.Campaign dc WITH (NOLOCK)
        ON dc.ServicerID = ap.ServicerID
            AND dc.ProcessTemplateID = ap.ProcessTemplateID
            AND dc.ProcessStageID = ap.ProcessStageID
WHERE ProcessStatusID = 1

-- update @open table if eligible for Skip trace

UPDATE  @Open
SET     ValidContactPhoneCount = ( SELECT   COUNT(cp.ContactPhoneID)
                                   FROM     @Open o
                                            INNER JOIN dbo.AccountContact ac ON ac.AccountID = o.AccountID 
                                            INNER JOIN dbo.Contact c ON c.ContactID = ac.ContactID
                                            INNER JOIN dbo.ContactPhone cp ON cp.ContactID = c.ContactID
                                   WHERE    ac.AccountID = o.AccountID
                                            AND cp.PhoneTypeID NOT IN ( 5, 8 )
                                 )
FROM    @Open o
        INNER JOIN dbo.AccountContact ac ON ac.AccountID = o.AccountID 
WHERE   ac.AccountID = o.AccountID


SELECT * FROM @Open

1 个答案:

答案 0 :(得分:1)

我还没有对此进行测试......但我怀疑问题出在您的派生表中。我更倾向于做类似的事情;

UPDATE  @Open
SET     ValidContactPhoneCount = CountData.ValidCount
FROM    @Open o
        INNER JOIN dbo.AccountContact ac ON ac.AccountID = o.AccountID 
        INNER JOIN ( SELECT   COUNT(cp.ContactPhoneID) as ValidCount, oAccountID
                                   FROM     @Open o
                                            INNER JOIN dbo.AccountContact ac ON ac.AccountID = o.AccountID 
                                            INNER JOIN dbo.Contact c ON c.ContactID = ac.ContactID
                                            INNER JOIN dbo.ContactPhone cp ON cp.ContactID = c.ContactID
                                   WHERE    ac.AccountID = o.AccountID
                                            AND cp.PhoneTypeID NOT IN ( 5, 8 )
                                            GROUP BY o.AccountID
                                 ) CountData ON CountData.AccountID = o.AccountID
WHERE   ac.AccountID = o.AccountID

通过这种方式,您可以按帐户ID对计数进行分组,然后按帐户引用它们。就像我说的那样,我还没有对此进行过测试,但是你在派生表中缺乏分组是你获得总数的原因。