我想用欠款帐户执行一批客户

时间:2014-07-24 18:09:50

标签: sql sql-server

我正在使用Microsoft SQL Server 2005,而我正在尝试在运行对delinqent客户的计数时过滤我们的重复客户名称。我有拖欠的合同号码设置为0-30-60-90-120-150-180篮子,但有些客户有多个拖欠合同,我确实想要数两次。

Select 
(Case
    When dbo.v_opened_contract_dropped_fields.days_past_due >= 180 Then 180
    When dbo.v_opened_contract_dropped_fields.days_past_due >= 150 Then 150
    When dbo.v_opened_contract_dropped_fields.days_past_due >= 120 Then 120
    When dbo.v_opened_contract_dropped_fields.days_past_due >= 90 Then 90
    When dbo.v_opened_contract_dropped_fields.days_past_due >= 60 Then 60
    When dbo.v_opened_contract_dropped_fields.days_past_due >= 30 Then 30
END) AS Fed_Dlq_Bucket, Count(dbo.customer.cust_name) AS CountOfCustomer_name, Count(dbo.contract.contract_id) AS CountOfcontract_id 
From dbo.contract INNER JOIN dbo.v_opened_contract_dropped_fields ON dbo.contract.contract_id = dbo.v_opened_contract_dropped_fields.contract_id 
WHERE (((v_opened_contract_dropped_fields.RELATIONSHIP) Like '00000001' Or (v_opened_contract_dropped_fields.RELATIONSHIP)='00000002' Or (v_opened_contract_dropped_fields.RELATIONSHIP)='00000011' Or (v_opened_contract_dropped_fields.RELATIONSHIP)='00000012' Or (v_opened_contract_dropped_fields.RELATIONSHIP)='00000021' Or (v_opened_contract_dropped_fields.RELATIONSHIP)='00000031' Or (v_opened_contract_dropped_fields.RELATIONSHIP)='00000041') AND ((v_opened_contract_dropped_fields.BLENDED_NET_INVEST)>0) AND ((contract.contract_status) Is Null Or (contract.contract_status)<>88))
GROUP BY 
    (Case
        When dbo.v_opened_contract_dropped_fields.days_past_due >= 180 Then 180
        When dbo.v_opened_contract_dropped_fields.days_past_due >= 150 Then 150
        When dbo.v_opened_contract_dropped_fields.days_past_due >= 120 Then 120
        When dbo.v_opened_contract_dropped_fields.days_past_due >= 90 Then 90
        When dbo.v_opened_contract_dropped_fields.days_past_due >= 60 Then 60
        When dbo.v_opened_contract_dropped_fields.days_past_due >= 30 Then 30
    END)
ORDER BY Fed_Dlq_Bucket;

我希望在每个存储桶中看到已过滤的逾期客户数量,且没有重复的客户名称。所有帮助表示赞赏!当我添加这个联接时,我的总数会上升,而不是下降......我无法理解。

Inner Join dbo.customer ON dbo.v_contract_all.customer_name = dbo.customer.cust_name

1 个答案:

答案 0 :(得分:0)

一些示例数据可能有助于找到解决问题的方法。

DECLARE @Contract TABLE
(
    CUSTOMER_NAME VARCHAR(10),
    DAYS_PAST_DUE INT
)

INSERT INTO @Contract
VALUES
( 'Fred', 100 ),
( 'George', 20 ),
( 'Mac', 30 ),
( 'Cheese', 40 ),
( 'Fred', 100 ),
( 'Blue', 50 ),
( 'Oswald', 60 ),
( 'Herman', 70 ),
( 'Fred', 100 ),
( 'Betty', 80 ),
( 'Wilma', 90 ),
( 'Oswald', 60 ),
( 'Barney', 190 );

我把弗雷德放在那里几次,因为威尔玛一直在使用他的充电卡。

WITH T AS
(
    SELECT *,
        (CASE 
            When days_past_due >= 180 Then 180
            When days_past_due >= 150 Then 150
            When days_past_due >= 120 Then 120
            When days_past_due >= 90 Then 90
            When days_past_due >= 60 Then 60
            When days_past_due >= 30 Then 30
            ELSE 0
        END) Fed_Dlq_Bucket,
        ROW_NUMBER() OVER (PARTITION BY c.CUSTOMER_NAME ORDER BY c.DAYS_PAST_DUE DESC) RN
    FROM @Contract c
)

这里我做一个CTE试图弄清楚每一行的过期桶,我也根据客户名称划分行,所以如果有重复,我只会看看每一行的第一行集。

SELECT Fed_Dlq_Bucket,
    COUNT(*) NumOfDelinquents
FROM T
WHERE T.RN = 1
GROUP BY T.Fed_Dlq_Bucket

我总结了桶并吐了总数:

Fed_Dlq_Bucket  NumOfDelinquents
0   1
30  3
60  3
90  2
180 1

也许你可以使用分区清除那些欺骗并让你的计数更容易?希望这会有所帮助。