CREATE TABLE #Donations100 ( [ID] INT, [Product1] VARCHAR...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Motor Vehicle Maintenance and Repair Meta

Q&A about the site for mechanics and DIY enthusiast owners of cars, trucks, and motorcycles

CREATE TABLE #Donations100 (
    [ID] INT,
    [Product1] VARCHAR(32),
    [Quantity1] INT,
    [Product2] VARCHAR(32),
    [Quantity2] INT,
    [Product3] VARCHAR(32),
    [Quantity3] INT
);
    
INSERT INTO #donations100 ([ID], [Product1], [Quantity1], [Product2], [Quantity2], [Product3], [Quantity3])
VALUES
    (1,'Blankets',3,'Hand Towels',5,'Queen Bed Frames',2),
    (2,'Dressers',10,'Queen Mattreses',2,null,null),
    (3,'Welcome Mats',2,'Silverware',6,null,null);

    
SELECT 
    d.ID,
    attrs.Item,
    CASE attrs.Item
        WHEN 'Product1' THEN d.Quantity1
        WHEN 'Product2' THEN d.Quantity2
        WHEN 'Product3' THEN d.Quantity3
    END AS Quantity
FROM
    #donations100 d
CROSS JOIN
(
    SELECT 'Product1' AS Item
    UNION ALL
    SELECT 'Product2'
    UNION ALL
    SELECT 'Product3'
) AS attrs
ORDER BY
    d.ID,
    attrs.Item;

Enter Parameters

Options:
Switch to main site
loading Hold tight while we fetch your results