希望你们都好。我需要进行查询。请帮助我。
以下是场景。我们有两个表
我想只获得过去三个月没有订购任何东西的客户
请帮助我。我被困了。提前致谢
答案 0 :(得分:3)
在不知道您的确切数据结构的情况下,我猜测的是:
Select CustomerCode,
CustomerName
From dbo.Customers
where CustomerCode Not in (
Select CustomerCode
From dbo.Orders
Where OrderDate > Cast(Floor(Cast(dateAdd(Month,-3, GetDate()) as Float))as DateTime)
)
答案 1 :(得分:2)
SELECT customer_number
FROM Customers
EXCEPT
SELECT customer_number
FROM Orders
WHERE DATEADD(MONTH,-3, CURRENT_TIMESTAMP) < order_date;
答案 2 :(得分:1)
;WITH CTE_LastOrder (CustomerId, LastOrderDate) As
(
SELECT CustomerId, MAX(OrderDate) LastOrderDate
FROM Orders
GROUP By CustomerId
)
SELECT * from Customers C
JOIN CTE_LastOrder LO ON C.CustomerId = LO.CustomerId
WHERE LO.LastOrderDate > Cast(Floor(Cast(dateAdd(Month,-3, GetDate()) as Float))as DateTime)
以上是SQL Server的基本sql。语法可能略有不同。