【问题标题】:Get the records which are not created this year using SQLSQL获取今年未创建的记录
【发布时间】:2022-11-18 07:55:15
【问题描述】:

我有两张桌子。一张是 Contact Registration 表,一张是 Payments 表 一个联系人注册可以有多个付款。例如:1 个 CR 可以有 15 个付款

ContactRegId RegStatus RenewalStatus ExpiryDate
123456 Suspended Pending 2023-02-15
2345678 Suspended Complet 2023-01-03
758948 Registerd Pending 2023-02-15
3134232 notregstd Pending 2023-01-03
435345 Registerd Pending 2023-02-15
5636443 notregstd Pending 2023-01-03
ContactRegId CreatedOn PaymentId
123456 2021-05-10 43
123456 2020-09-11 42
123456 2019-03-30 21
123456 2018-12-10 46
758948 2022-09-14 24
758948 2021-08-17 423
758948 2020-07-03 432
758948 2019-06-23 437
758948 2018-09-15 138
435345 2022-09-11 2345
435345 2021-08-01 4234
435345 2020-07-12 436
435345 2019-04-14 438
435345 2018-01-16 433

我需要满足 (RegStatus = Suspended or registered) 和 RenewalStatus = Pending from Contact registration table 的 Contact Registration 记录 如果今年没有创建付款

所以输出应该是

ContactRegId RegStatus RenewalStatus ExpiryDate
123456 Suspended Pending 2023-02-15

这个怎么做?

【问题讨论】:

  • 你尝试过什么吗?这是一个非常基本的查询,那里有很多很好的教程。

标签: sql sql-server


【解决方案1】:

我需要满足 (RegStatus = Suspended or registered ) 和 RenewalStatus = Pending from Contact registration table 的 Contact Registration 记录,如果今年没有创建付款

您可以使用子查询过滤注册表,以确保联系人今年没有付款:

select r.*
from contactRegistration r
where r.RegStatus in ('Suspended', 'Registered')
    and r.RenewalStatus = 'Pending'
    and not exists (
        select 1
        from payments p
        where 
            p.ContactRegId = r.ContactRegId 
            and p.CreatedOn >= datefromparts(year(getdate()), 1, 1)
    )

【讨论】:

    猜你喜欢
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多