案例陈述,其中重复的ID具有不同的状态,我无法按照我的意愿返回

时间:2017-01-12 14:00:12

标签: sql sql-server

我真的希望有人可以帮我解决这个问题。

情景:

新栏目(案例陈述):

当前答案为IN

('自动拒绝','在AIP退出之前','在AIP退出后退出','撤回银行',' ;在AIP'之后撤回原因,在AIP之前撤回原因')

然后应该说'休眠'

当前回答 ('报价收到 - 等待接受', 收到报价 - 等待接受', '发送给银行的更多信用信息 - 等待AIP',' AIP接受 - 等待报价', '顾问待定 - 等待反馈', '发送给银行的更多信用信息 - 等待报价', 收到AIP报价 - 正在等待报价', '等待接受报价', ' Sect Title Register未打开 - 等待关闭', '待售 - 等待关闭', '需要进一步的信用信息', 仲裁 - 等待AIP', ' SFM Credit Appeal', '等待修改报价', '等待AIP', 收到AIP - 需要更多信用信息', 收到AIP报价 - 等待接受', 仲裁 - 等待报价', 收到AIP - 等待接受', 收到AIP - 等待报价', '频道冲突', '没有AMS')

然后应该说'有效'

这是真正的问题:

当该ID仅具有休眠状态时,它应该显示休眠状态,但是当一个ID在活动答案上有一个或多个时,该ID的所有记录都应该是活动的。 为了让您了解我得到的结果:

如果你不能看到这里的图片是我得到的结果:

ApplicationID |  AnswerType         |REFStatus
1001145       |Decline Before AIP   |Dormant
1001145       |Decline Before AIP   |Dormant
1001145       |WithDrawn by Bank    |Dormant
1060906       |Decline Before AIP   |Dormant
1060906       |Decline Before AIP   |Dormant
1060906       |Decline Before AIP   |Dormant
1060906       |SFM Credit Appeal    |Active

以下是我要找的结果:

ApplicationID |  AnswerType         |REFStatus
1001145       |Decline Before AIP   |Dormant
1001145       |Decline Before AIP   |Dormant
1001145       |WithDrawn by Bank    |Dormant
1060906       |Decline Before AIP   |Active
1060906       |Decline Before AIP   |Active
1060906       |Decline Before AIP   |Active
1060906       |SFM Credit Appeal    |Active

CODE:

SELECT app.ApplicationID, bankans.AnswerType,
--This CASE is fine---
CASE 
WHEN bankans.AnswerType IN ('Auto Decline',
                            'Decline Before AIP',
                            'Decline After AIP', 
                            'Withdrawn by Bank',
                            'Withdrawn By Origination After AIP',
                            'Withdrawn By Origination Before AIP')
THEN 'Dormant'

---Here is where my problem lies. Getting all the rows of the ID to say Active, when the ID has at least one active status.

WHEN bankans.AnswerType IN ('Auto Decline',
                            'Decline Before AIP',
                            'Decline After AIP', 
                            'Withdrawn by Bank',
                            'Withdrawn By Origination After AIP',
                            'Withdrawn By Origination Before AIP')
---These are the Answers that would have an Active REFStatus below
    OR bankans.AnswerType IN ('Quotation Offer Received - Awaiting Acceptance',
                                'Quotation Received - Awaiting Acceptance',
                                'Further Credit Info Sent To Bank - Awaiting AIP',
                                'AIP Accepted - Awaiting Quotation',
                                'Consultant Pending - Awaiting Feedback',
                                'Further Credit Info Sent To Bank - Awaiting Quotation',
                                'AIP Offer Received - Awaiting Quotation',
                                'Pending Acceptance Of Quotation',
                                'AIP Received Awaiting Quotation',
                                'Sect Title Register Not Opened - Awaiting Closure',
                                'Subject To Sale - Awaiting Closure',
                                'Further Credit Info Required', 'SFM Credit Appeal')

                                THEN 'Active'

                                END AS 'REFStatus'

FROM Import.OobaApplication AS app
LEFT OUTER JOIN Import.OobaBankAnswer AS bankans ON app.ApplicationID = bankans.ApplicationID
ORDER BY app.ApplicationID

我真的很感激帮助。如果您可能需要任何其他信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

试试这个:

declare @App table (ApplicationID int)
insert @App values (1001145),(1060906)

declare @AppAnswer table (ApplicationID int, AnswerType varchar(max))
insert @AppAnswer values (1001145, 'Decline Before AIP'), (1001145, 'Withdrawn by Bank'), (1001145, 'Decline Before AIP'),
                         (1060906, 'Decline Before AIP'), (1060906, 'Decline Before AIP'), (1060906, 'SFM Credit Appeal')

;with CTE (ApplicationID, AnswerType, Stat) As 
(
    select app.ApplicationID, bankans.AnswerType, 
           CASE WHEN bankans.AnswerType IN ('Auto Decline',
                            'Decline Before AIP',
                            'Decline After AIP', 
                            'Withdrawn by Bank',
                            'Withdrawn By Origination After AIP',
                            'Withdrawn By Origination Before AIP')
                THEN 'Dormant'
                ELSE 'Active' 
            END AS Stat
    FROM @App AS app
    LEFT OUTER JOIN @AppAnswer AS bankans ON app.ApplicationID = bankans.ApplicationID  
)
SELECT ApplicationID, AnswerType, IIF(count(*) over (partition by ApplicationID) > count(Stat) over (partition by ApplicationID, Stat), 'Active', 'Dormant')
FROM CTE 

If语句检查count状态是否与ID的count不同。