Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tsql 如何从表1中选择某些日期之间表2中没有记录的记录_Tsql - Fatal编程技术网

Tsql 如何从表1中选择某些日期之间表2中没有记录的记录

Tsql 如何从表1中选择某些日期之间表2中没有记录的记录,tsql,Tsql,我有一张客户表和另一张与之相关的销售活动表(电话、电子邮件和ecc)。 我需要知道的是,这些客户中有哪些在过去30天内没有联系(没有活动) 以下是我想做的: SELECT crm_customers.id, crm_ customers.companyname FROM crm_ customers LEFT JOIN crm_activities on crm_customers.id = crm_ activities. Customerid WHERE crm_ activities.c

我有一张客户表和另一张与之相关的销售活动表(电话、电子邮件和ecc)。 我需要知道的是,这些客户中有哪些在过去30天内没有联系(没有活动)

以下是我想做的:

SELECT crm_customers.id, crm_ customers.companyname
FROM crm_ customers 
LEFT JOIN crm_activities on crm_customers.id = crm_ activities. Customerid
WHERE crm_ activities.createddate < (getDate() – 30)
选择crm\u customers.id、crm\u customers.companyname
来自crm客户
在crm_customers.id=crm_活动上左键加入crm_活动。客户ID
其中crm_uuActivities.createddate<(getDate()–30)
问题是,即使客户最近有活动,它也会返回超过30天的活动。我只需要得到过去30天内没有任何销售活动的客户
感谢您的帮助

这可能会帮助您:使用子查询:

select 
    cust.id, cust.compayname
from
    crm_customers as cust
    left join (
        select 
            * 
        from 
            crm_activities 
        where 
            createddate >= (getDate() - 30)
    ) as act on cust.id = act.customerId
where
    act.customerId is null
这样,您就排除了过去30天内有活动的所有客户


您可能希望通过执行以下操作来进一步清理此问题:

select 
    cust.id, cust.compayname
from
    crm_customers as cust
    left join (
        select distinct 
            customerId 
        from 
            crm_activities where createddate >= (getDate() - 30)
    ) as act on cust.id = act.customerId
where
    act.customerId is null
这是相同的基本想法。。。唯一的问题是,第二个选项只返回带有活动的客户的ID。我认为这可能会优化外部
where
子句,但它可能会对子查询(无免费午餐)的性能产生影响
选择crm\u customers.id、crm\u customers.companyname
来自crm_客户
左键加入crm_活动
关于crm_customers.id=crm_activities.Customerid
和DATEDIFF(DD,crm_activities.createddate,getdate())<30
其中crm_activities.Customerid为空
这是连接中的条件与其中的条件不同的情况

为了提高速度,我会怀疑子查询上的连接。它为查询优化器提供了更多的优化选项。不需要一个明确的名称。假设crm_activities.Customerid已编制索引,这将在索引上加入。对于子查询,它将使用索引创建列表,但子查询的输出没有索引

将取决于你的数据。我有一张大桌子要测试

查询计划是不同的

在一个环形接头上,温度很低 在散列连接上,速度要快一倍 在合并连接上,速度是原来的十倍

如果没有排除,则为死热。

如果排除了许多客户(在过去30年中已联系过),则会出现此问题。

提示:您可能想要扭转测试的感觉-排除最近有活动的客户这一点也很有效。另一个具有相同结果的选项。我想知道是否有性能差异。thanks@DaKodar我对性能比较很好奇。当你拥有它们时,你会分享它们吗?@DaKodar请看我的评论。子查询使索引在联接上失效。在这两个数据库上签出一个查询计划。@barranka确定。我将使用大数据运行一些测试,并让您知道结果;)同样基于crm_customers.id和crm_activities.Customerid上的索引
SELECT crm_customers.id, crm_customers.companyname
FROM crm_customers 
LEFT JOIN crm_activities 
  on crm_customers.id = crm_activities.Customerid
 and DATEDIFF(DD, crm_activities.createddate, getdate()) < 30
where crm_activities.Customerid is null