查找过期的信用卡

时间:2018-01-05 20:06:58

标签: sql-server subquery

我有一个查询,查找客户的最后一个信用卡到期日期。客户可能会有多张信用卡存档。

这很有用。 lastcc是YYMM格式。

SELECT
dbo.xccard.CustId, 'xxxxx' as Name,
Max(substring(ExpDate,3,2) +
substring(ExpDate,1,2)) AS lastcc
FROM
dbo.xccard
INNER JOIN dbo.Customer ON dbo.Customer.CustId = dbo.xccard.CustId
INNER JOIN dbo.SOHeader ON dbo.Customer.CustId = dbo.SOHeader.CustID

WHERE SOHeader.Status <> 'C'
and Customer.Status ='A'
and Terms = 'CC'

GROUP BY
dbo.xccard.CustId,
dbo.Customer.Name

ORDER BY lastcc

我想将结果限制为lastcc小于当前月份的结果。

我认为我需要做一个子查询,以便根据今天的YYMM从上面的查询生成的表中搜索,但我完全迷失了。

这样的东西
Select CustId, Name, lastcc in (

SELECT
    dbo.xccard.CustId, 'xxxxx' as Name,
    Max(substring(ExpDate,3,2) +
    substring(ExpDate,1,2)) AS lastcc
    FROM
    dbo.xccard
    INNER JOIN dbo.Customer ON dbo.Customer.CustId = dbo.xccard.CustId
    INNER JOIN dbo.SOHeader ON dbo.Customer.CustId = dbo.SOHeader.CustID

    WHERE SOHeader.Status <> 'C'
    and Customer.Status ='A'
    and Terms = 'CC'

    GROUP BY
    dbo.xccard.CustId,
    dbo.Customer.Name

    ORDER BY lastcc
    )

WHERE lastcc > '1701'

我也尝试过:

我也试过这个

Select CustId, Name, lastcc in (



WHERE lastcc > '1701'

AND
dbo.xccard_all_FullLookUp.CustId in (
SELECT
    dbo.xccard.CustId, 'xxxxx' as Name,
    Max(substring(ExpDate,3,2) +
    substring(ExpDate,1,2)) AS lastcc
    FROM
    dbo.xccard
    INNER JOIN dbo.Customer ON dbo.Customer.CustId = dbo.xccard.CustId
    INNER JOIN dbo.SOHeader ON dbo.Customer.CustId = dbo.SOHeader.CustID

    WHERE SOHeader.Status <> 'C'
    and Customer.Status ='A'
    and Terms = 'CC'

    GROUP BY
    dbo.xccard.CustId,
    dbo.Customer.Name


    )

有人可以帮忙吗?非常感谢

0 个答案:

没有答案