79279518

Date: 2024-12-13 20:41:22
Score: 1.5
Natty:
Report link

@Barmar brings up a good point that product id and names should be grouped together. The following code makes that assumption, and that id/names are not unordered:

WITH RankedAttributes AS (
    SELECT 
        Value AS ProductValue,
        ROW_NUMBER() OVER (ORDER BY NULL) AS rn,
        Attribute
    FROM your_table_name
),
GroupedAttributes AS (
    SELECT 
        MAX(CASE WHEN Attribute = 'product_id' THEN ProductValue END) AS product_id,
        MAX(CASE WHEN Attribute = 'product_name' THEN ProductValue END) AS product_name
    FROM RankedAttributes
    GROUP BY (rn - ROW_NUMBER() OVER (PARTITION BY Attribute ORDER BY rn))
)
SELECT product_id, product_name
FROM GroupedAttributes
WHERE product_id IS NOT NULL;
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Barmar
  • Looks like a comment (1):
  • Low reputation (0.5):
Posted by: Silver Spade