0

我需要列出每个客户完成的所有订单,所以如果某个客户已经通过了 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 `

有没有人知道我将如何正确地做到这一点?

4

2 回答 2

1

通过使用 SQL Server 2005 及更高版本的 XML 功能,您可以轻松有效地生成逗号分隔值

试试这个(根据你的表和列名调整):

-- Sample tables
declare @Customer table
(
  CustomerId int,
  Name varchar(50)
)

declare @Order table
(
  OrderId int,
  CustomerId int
)

-- Sample data
insert into @Customer (CustomerId, Name) values (1, 'Alice')
insert into @Customer (CustomerId, Name) values (2, 'Mary')
insert into @Customer (CustomerId, Name) values (3, 'David')

insert into @Order (OrderId, CustomerId) values (1, 1)
insert into @Order (OrderId, CustomerId) values (2, 1)
insert into @Order (OrderId, CustomerId) values (3, 1)
insert into @Order (OrderId, CustomerId) values (4, 2)
insert into @Order (OrderId, CustomerId) values (5, 2)
insert into @Order (OrderId, CustomerId) values (6, 3)

----select * from @Customer
----select * from @Order

/* Actual Query that would add the OrderId from @Order table as a comma separated list against relevant Customer */
SELECT Customer.CustomerId as CustomerId, Customer.Name,
        (
        SELECT   Convert(varchar(1), O.OrderId) + ','
        FROM     @Order AS O
        WHERE    O.CustomerId = Customer.CustomerId
        ORDER BY O.OrderId
        FOR XML PATH('')
        ) AS OrderList
FROM   @Customer AS Customer
于 2013-06-03T12:32:52.757 回答
0

好吧,我不能给你一个完整的答案,但我可以告诉你如何解决它。尝试使用公用表表达式 ( http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx )。

有了这个,你可以加入你自己的结果集,在那里你可以创建一个递归循环来解决这个问题。

希望对你有帮助

于 2013-06-03T12:25:54.230 回答