Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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
Sql server 返回客户的T-SQL搜索_Sql Server_Tsql - Fatal编程技术网

Sql server 返回客户的T-SQL搜索

Sql server 返回客户的T-SQL搜索,sql-server,tsql,Sql Server,Tsql,我有一个问题,我现在在哪里挣扎。 我真的不知道如何解决这个问题,它看起来很简单 我有客户ID和订单日期。 我只想向客户展示,他们在2015年之前订购了东西,并在过去10天内购买了一些东西 我为此创建了一个小测试表——假设是2016年1月: 现在有一个客户1,在2010年1月和1月进行了购买。好的,这符合我的需要,我想让他看看。 但是客户2在去年12月买了东西,所以他不是一个“退货”客户,而是一个经常买我东西的客户。我不想给他看 我试过这样的方法,但没有成功: SELECT [Kunden_ID

我有一个问题,我现在在哪里挣扎。 我真的不知道如何解决这个问题,它看起来很简单

我有客户ID和订单日期。 我只想向客户展示,他们在2015年之前订购了东西,并在过去10天内购买了一些东西

我为此创建了一个小测试表——假设是2016年1月:

现在有一个客户1,在2010年1月和1月进行了购买。好的,这符合我的需要,我想让他看看。 但是客户2在去年12月买了东西,所以他不是一个“退货”客户,而是一个经常买我东西的客户。我不想给他看

我试过这样的方法,但没有成功:

SELECT [Kunden_ID],Bestellung
FROM [Immo].[dbo].[TEST] AS A
WHERE (Bestellung >=DATEADD (day,-10,getdate()) 
AND Bestellung <= DATEADD (month,-12,getdate()))
SELECT[Kunden_ID],Bestellung
从[Immo].[dbo].[TEST]作为
其中(Bestellung>=DATEADD(day,-10,getdate())

和Bestellung您需要两个单独的查询。第一个查询查找过去10天内购买了东西的客户。第二个查询使用exists查询查找12个月前购买了东西的客户(使用ID加入)

试试这个:

SELECT [Kunden_ID],Bestellung
FROM [Immo].[dbo].[TEST] AS A
WHERE (Bestellung >=DATEADD (day,-10,getdate())) 
and exists (
select 1
from [Immo].[dbo].[TEST] AS B
where a.[Kunden_ID] = b.[Kunden_ID]
AND b.Bestellung <= DATEADD (month,-12,getdate())
)
SELECT[Kunden_ID],Bestellung
从[Immo].[dbo].[TEST]作为
其中(Bestellung>=DATEADD(day,-10,getdate())
存在(
选择1
从[Immo].[dbo].[TEST]作为B
其中a.[Kunden_ID]=b.[Kunden_ID]
b.贝斯特隆
With Get10Days as (
SELECT [Kunden_ID],Bestellung
FROM [Immo].[dbo].[TEST] AS A
WHERE (Bestellung >=DATEADD (day,-10,getdate())) 
)


select b.Kunden_ID
from [Immo].[dbo].[TEST] AS B
join Get10Days as A on a.Kunden_ID = b.Kunden_ID
where b.Bestellung <= DATEADD (month,-12,getdate())