我需要列出每个客户完成的所有订单,所以如果某个客户已经通过了 6 个订单,那么我们需要列出 order1 | 订单2| 订单3| ...
我有一个名为 Order 的表,其中我们有客户端 ID,我使用的是 SqlServer 2008 R2,我尝试了不同的方法但没有成功,它们永远运行并且从未给出结果:s,这是我的最新尝试:
SELECT convert(varchar(1),isnull(user.Order.OrderId,'')) +' | '+ convert(varchar(1),isnull(Order_1.OrderId,'')) +' | '+convert(varchar(1),isnull(Order_2.OrderId,'')) +' | '+convert(varchar(1),isnull(Order_3.OrderId,'')) +' | '+convert(varchar(1),isnull(Order_4.OrderId,'')) +' | '+convert(varchar(1),isnull(Order_5.OrderId,'')) as OrderIdList,
user.client.kdnr
FROM user.Order
left outer JOIN user.Order AS Order_1 ON user.Order.clientnr = Order_1.clientnr AND Order_1.OrderId <> user.Order.OrderId and Order.orderDate < Order_1.orderDate
left outer JOIN user.Orderold AS Order_2 ON user.Order.clientnr = Order_2.clientnr AND Order_2.OrderId not in ( user.Order.OrderId , Order_1.OrderId ) and Order_1.orderDate < Order_2.orderDate
left outer JOIN user.Orderold AS Order_3 ON user.Order.clientnr = Order_3.clientnr AND Order_3.OrderId not in ( user.Order.OrderId , Order_1.OrderId, Order_2.OrderId ) and Order_1.orderDate < Order_2.orderDate
left outer JOIN user.Orderold AS Order_4 ON user.Order.clientnr = Order_4.clientnr AND Order_4.OrderId not in ( user.Order.OrderId , Order_1.OrderId, Order_2.OrderId, Order_3.OrderId ) and Order_2.orderDate < Order_3.orderDate
left outer JOIN user.Orderold AS Order_5 ON user.Order.clientnr = Order_5.clientnr AND Order_5.OrderId not in ( user.Order.OrderId , Order_1.OrderId, Order_2.OrderId, Order_3.OrderId, Order_4.OrderId ) and Order_3.orderDate < Order_4.orderDate
INNER JOIN user.client ON Order_1.clientnr = user.client.kdnr
group by client.kdnr ,user.client.name1, user.client.firstname, user.Order.OrderId, Order_1.OrderId,Order_1.Ordernr,Order_2.OrderId ,Order_3.OrderId,Order_4.OrderId,Order_5.OrderId `
有没有人知道我将如何正确地做到这一点?