This is another way to and its a little more dynamic, and saves the
DECLARE @CustOrder TABLE
(
CustId Int
,SalesValue Money
)
INSERT INTO @CustOrder
(CustId,SalesValue)
Values
(100813, 124)
,(100813, 110)
,(100794, 297)
,(100794, 99)
,(100794, 101);
WITH CTE_data
As
(
SELECT ROW_NUMBER() OVER (PARTITION BY CustId Order by CustId) rn
,CustId
,SalesValue
From @CustOrder
)
,CTE
AS
(
SELECT
CustId
,Max(CASE rn WHEN 1 THEN 1 ELse 1 END) Number1
,Max(CASE rn WHEN 1 THEN SalesValue ELse 0 END) Salesvalue1
,Max(CASE rn WHEN 2 THEN 2 ELse 2 END) Number2
,Max(CASE rn WHEN 2 THEN SalesValue ELse 0 END) Salesvalue2
,Max(CASE rn WHEN 3 THEN 3 ELse 3 END) Number3
,Max(CASE rn WHEN 3 THEN SalesValue ELse 0 END) Salesvalue3
From CTE_Data
Group by CustId
)
SELECT
Number,CustId, SalesValue
FROM
CTE
CROSS APPLY
(VALUES
(Number1,SalesValue1)
,(Number2,SalesValue2)
,(Number3,SalesValue3)
) x (Number,SalesValue)
Not sure of the performance on a very large table but splitting out the first CTE that adds a row number into a Temporary table.
_________________________________________________________________________
SSC Guide to Posting and Best Practices