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 に答える