2

Below is sample data and expected results:

Table1: salesprice

ItemNo Variant SalesCode UnitPrice
 A 1 SRP 100
 A 2 SRP 200
 A 1 WSP 50
 A 2 WSP 100
 B 1 SRP 300
 B 2 SRP 400
 B 1 WSP 150
 B 2 WSP 200

Query Results:

ItemNo Variant SalesCode(SRP) UnitPrice SalesCode(WSP) UnitPrice
 A 1 SRP 100 WSP 50 
 A 2 SRP 200 WSP 100
 B 1 SRP 300 WSP 150
 B 2 SRP 400 WSP 200
Paul White
95.3k30 gold badges439 silver badges689 bronze badges
asked Oct 16, 2014 at 9:07
0

2 Answers 2

3
CREATE TABLE #Data
 (
 ItemNo VARCHAR(10),
 Variant INT,
 SalesCode VARCHAR(30),
 UnitPrice INT
 )
GO
INSERT INTO #Data
 ( ItemNo, Variant, SalesCode, UnitPrice )
VALUES ( 'A', 1, 'SRP', 100 ),
 ( 'A', 2, 'SRP', 200 ),
 ( 'A', 1, 'WSP', 50 ),
 ( 'A', 2, 'WSP', 100 ),
 ( 'B', 1, 'SRP', 300 ),
 ( 'B', 2, 'SRP', 400 ),
 ( 'B', 1, 'WSP', 150 ),
 ( 'B', 2, 'WSP', 200 )
GO
SELECT ItemNo, Variant, 'SRP' AS SalesCode_SRP,
 SUM(CASE SalesCode
 WHEN 'SRP' THEN UnitPrice
 ELSE 0
 END) UnitPrice_SRP, 'WSP' AS SalesCode_WSP,
 SUM(CASE SalesCode
 WHEN 'WSP' THEN UnitPrice
 ELSE 0
 END) UnitPrice_WSP
FROM #Data
GROUP BY ItemNo, Variant 
GO
DROP TABLE #Data
answered Oct 16, 2014 at 9:28
1
  • Just needs an ORDER BY ItemNo, Variant to exactly match the desired output Commented Oct 16, 2014 at 10:27
1
select d1.*, d2.SalesCode, d2.UnitPrice 
from Data d1, Data d2 
where d1.Variant = d2.Variant and d1.ItemNo = d2.ItemNo 
 and d1.SalesCode = 'SRP' 
 and d2.SalesCode = 'WSP';
answered Oct 16, 2014 at 13:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.