Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/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
使用sql显示最近放弃的订单_Sql_Sql Server 2005 - Fatal编程技术网

使用sql显示最近放弃的订单

使用sql显示最近放弃的订单,sql,sql-server-2005,Sql,Sql Server 2005,我有三张桌子:顾客、产品、订单 我试图识别所有创建了废弃订单(订单状态!=“已完成”)但没有其他订单已完成(订单状态!=“已完成”)的客户 我有下面的查询,它返回所有符合此标准的订单,但是有多个来自不同订单的客户实例,我只需要该客户的一个实例,最好是最近被放弃的订单 SELECT c.Id as Id, c.Name as FullName, o.Id as Order_Id, o.OrderStatus, o.OrderDate, o.PaidByCC, p.ProductStatus, Fr

我有三张桌子:顾客、产品、订单

我试图识别所有创建了废弃订单(订单状态!=“已完成”)但没有其他订单已完成(订单状态!=“已完成”)的客户

我有下面的查询,它返回所有符合此标准的订单,但是有多个来自不同订单的客户实例,我只需要该客户的一个实例,最好是最近被放弃的订单

SELECT
c.Id as Id,
c.Name as FullName,
o.Id as Order_Id,
o.OrderStatus,
o.OrderDate,
o.PaidByCC,
p.ProductStatus,
From Orders o
Join Customers c
On c.Id = o.CustomerId
Join Product p
On o.ProductId = p.[Product ID]
WHERE o.Type = 'Service'
    AND o.PaidByCC = 0
    AND o.OrderStatus IS NULL
    AND p.State = 'available'
    AND CONVERT(date, o.OrderDate) >= Convert(date, DATEADD(day, -30, GETDATE()))
    AND NOT EXISTS (Select o1.Id
                    From Orders o1
                    Where o1.OrderStatus = 'Placed'
                    AND o.CustomerId = o1.CustomerId)                    
我怎样才能做到这一点?

以下是我所拥有的: 必须将其放在临时表中,因为sql 2005不支持cte

    SELECT
    c.Id as Id,
    c.Name as FullName,
    o.Id as Order_Id,
    o.OrderStatus,
    o.OrderDate,
    o.PaidByCC,
    p.ProductStatus,
    From Orders o
    Join Customers c
    On c.Id = o.CustomerId into #AllAbandoned
    Join Product p
    On o.ProductId = p.[Product ID]
    WHERE o.Type = 'Service'
        AND o.PaidByCC = 0
        AND o.OrderStatus IS NULL
        AND p.State = 'available'
        AND CONVERT(date, o.OrderDate) >= Convert(date, DATEADD(day, -30, GETDATE()))
        AND NOT EXISTS (Select o1.Id
                        From Orders o1
                        Where o1.OrderStatus = 'Placed'
                        AND o.CustomerId = o1.CustomerId)  
-- Here we only want the abandoned
        AND order_status != 'completed'
--在这里,我们说,得到一个是最新的

Select * from #AllAbandoned a where not exists(Select 1 From #AllAbandoned b where a.Id = b.Id and a.OrderDate < b.OrderDate)
Select*from#AllAbandoned a,其中不存在(从#AllAbandoned b中选择1,其中a.Id=b.Id,a.OrderDate
SQL Server 2005?升级的时间是很多很多年前。您应该使用受支持的版本。查询语言是基于server 2005的专有语言。如果客户没有放弃的订单,您是否仍希望在输出中看到他?@zip否,仅限拥有放弃订单的客户。