在SQL Server中不使用

在SQL Server中不使用,sql,sql-server,Sql,Sql Server,我有一个查询,其中我试图找到一个数据库中有多少客户存在于另一个数据库中 select customerid from transactions where dtcreated > '5/2/14' in (Select customerid from database2.dbo.customers where dtcreated > '3/1/14

我有一个查询,其中我试图找到一个数据库中有多少客户存在于另一个数据库中

select customerid 
from transactions 
where dtcreated > '5/2/14'  in (Select customerid 
                                from database2.dbo.customers 
                                where dtcreated > '3/1/14')
Group by customerid
这个查询工作得很好。但很明显,有些客户在我在交易中搜索的时间段内没有交易。我在找那些顾客的身份证

我试过:

Select customerid 
from database2.dbo.customers 
where dtcreated > '3/1/14' NOT IN (select customerid 
                                   from transactions 
                                   where dtcreated > '5/2/14')
group by customerid

但是此查询不会返回任何结果。

这两个查询都不应该工作,因为您现在已经有了它们。您尚未指定子查询中的字段。将其修改为类似这样的内容

Select c.customerid from database2.dbo.customers c where c.dtcreated > '3/1/14' and c.customerid NOT  in
    (

    select customerid from transactions where created > '5/2/14'
    )
    group by c.customerid

如果您的客户ID相同,您可以在表上进行内部联接,然后只需向下筛选到所需的结果集。应该是一个更好的方式来实现你的要求交叉口

select database2.dbo.customers.customerid
from transactions trans
   INNER join database2.dbo.customers
      on trans.customerid = database2.dbo.customers.customerid
where trans.dtcreated < '5/2/14' 
   and database2.dbo.customers.dtcreated > '3/1/14'

Group by database2.dbo.customers.customerid
选择database2.dbo.customers.customerid
从交易到交易
内部联接数据库2.dbo.customers
在trans.customerid=database2.dbo.customers.customerid上
其中创建的trans.dt<'5/2/14'
和database2.dbo.customers.dtcreated>“3/1/14”
分组依据数据库2.dbo.customers.customerid
显然,这类操作有一个关键字

SELECT customerid
FROM transactions
WHERE dtcreated < '5/2/14' 

INTERSECT

SELECT customerid
FROM database2.dbo.customers
WHERE dtcreated > '3/1/14'
选择customerid
来自交易
其中创建的DTD<'5/2/14'
横断
选择customerid
来自database2.dbo.customers
其中创建了“3/1/14”

我认为问题是由于
日期
格式使用了以下内容:

Select customerid 
from database2.dbo.customers 
where CAST(dtcreated  as DATE) > '2014-01-03' NOT IN (select customerid 
                                   from transactions 
                                   where CAST(dtcreated  as DATE) > '2014-02-05')
group by customerid

我认为您需要更正这两个查询的语法。根据我的解释,您正在尝试:

Select customerid 
from database2.dbo.customers 
where dtcreated > '3/1/14' 
and customerid NOT IN (select customerid 
                       from transactions 
                       where dtcreated > '5/2/14')
无论您在子查询中返回哪个字段,都应该返回您说“不在”的字段。此外,由于您只返回客户ID,因此最后不需要group by。这将向客户返回一个dbo。记录日期晚于“3/1/14”的客户不在交易记录表中的日期大于“5/2/14”的客户。我希望这就是你想要的

另一种可能对您有用的选择是“不存在”不存在有时性能更好。有关更多详细信息,请参阅:

这看起来像:

Select customerid 
from database2.dbo.customers c
where dtcreated > '3/1/14' 
and not exists(select *                           
               from transactions t
               where dtcreated > '5/2/14'
               and c.customerid  = t.customerid
               )

我认为您没有得到结果,因为在客户id列中有一个空值

 Select distinct customerid 
    from database2.dbo.customers 
    where dtcreated> '2014-01-03' and 
    customerid NOT IN 
              (
                 select customerid 
                 from transactions 
                 where dtcreated > '2014-02-05' and 
                 trim(customerid) is not null
              )

如果需要有意义的结果,请将该字符串转换为日期每个数据库中的客户ID都相同?where Cast(dtcreated as datetime)>Cast('3/1/14'as datetime)没有区别。dtcreated列的类型为datetime。同样的问题。没有结果。当您单独运行子查询时,是否会得到结果?另外,你能用datetime变量代替字符串吗?我能,但是dtcreated列是dtcreated类型,所以这不重要。你应该至少加上两三句话来解释你的建议,以及为什么。。。