如何使用两个连接来运行递归查询



我正在尝试运行一个查询,其中我收到客户的名称,运送日期,订单日期以及迟到的天数。只有在迟到大于10天的时间时,才能显示所有这些。以下是我到目前为止的代码和结果。

唯一的问题是,只有一行应该显示,但它显示了数据库中存在的每个客户。这似乎是一个简单的修复程序,但我无法弄清楚。任何帮助都将受到赞赏。谢谢。

Execute:
select Customers.customerName as 'customer name',
shipdate.shippedDate as 'ship date',
shipdate.orderDate as 'order date',
datediff (shipdate.shippedDate, shipdate.orderDate) as 'days late'
from Customers join Orders 
on Customers.customerNumber = Orders.customerNumber

join Orders as shipdate join Orders as orderday 
on shipdate.shippedDate = orderday.shippedDate
where datediff (shipdate.shippedDate, shipdate.orderDate) > 10
    + ------------------ + -------------- + --------------- + -------------- +
| customer name      | ship date      | order date      | days late      |
+ ------------------ + -------------- + --------------- + -------------- +
| Online Diecast Creations Co. | 2003-12-26 00:00:00 | 2003-10-22 00:00:00 | 65             |
| Blauer See Auto, Co. | 2003-12-26 00:00:00 | 2003-10-22 00:00:00 | 65             |
| Vitachrome Inc.    | 2003-12-26 00:00:00 | 2003-10-22 00:00:00 | 65             |
| Baane Mini Imports | 2003-12-26 00:00:00 | 2003-10-22 00:00:00 | 65             |
| Euro+ Shopping Channel | 2003-12-26 00:00:00 | 2003-10-22 00:00:00 | 65             |
| Danish Wholesale Imports | 2003-12-26 00:00:00 | 2003-10-22 00:00:00 | 65             |
| Rovelli Gifts      | 2003-12-26 00:00:00 | 2003-10-22 00:00:00 | 65             |
| Land of Toys Inc.  | 2003-12-26 00:00:00 | 2003-10-22 00:00:00 | 65             |
| Cruz & Sons Co.    | 2003-12-26 00:00:00 | 2003-10-22 00:00:00 | 65             |

应该尝试这样的事情:

SELECT Customers.customerName AS customer_name,
       Orders.shippedDate AS ship_date,
       Orders.orderDate AS order_date,
       DATEDIFF( Orders.shippedDate, Orders.orderDate ) AS days_late
FROM Customers
  JOIN Orders ON Customers.customerNumber = Orders.customerNumber
WHERE DATEDIFF( Orders.shippedDate, Orders.orderDate ) > 10

相关内容

  • 没有找到相关文章

最新更新