sqlamazon-redshiftaginity

Forcing the color_code output to be a different string using CASE


So I was successfully able to pull the data that I need, however the issue now is that the color_code is a 2-digit number that not everyone knows. So, to rectify, I'd like to create a CASE statement that would replace each of these 84 unique color codes to a particular color (ex. 69 = Navy). To get the color code, I'm using a SUBSTRING on product_sku.

The real issue I'm having here is that I'm trying to paste this CASE statement I made into my query, but keep getting syntax errors at or near CASE:

(CASE
    WHEN SUBSTRING(product_sku,10,2) = 09 THEN SUBSTRING(product_sku,10,2) = 'Black'
    ELSE 'CHECK THIS SHIET FOO'
    FROM l_nvr_ec_returns
END)

This is just for one color for testing purposes, of course, but what am I doing wrong? Or perhaps more importantly, where exactly does this snippet even go? After my initial SELECT clause? SOS!

Thank you, Z

After some help from Caius Jard below, this is where I'm at. But getting an "INVALID SYNTAX" error

SELECT
    item_name,
    (SELECT
  CASE SUBSTRING(product_sku,10,2)
    WHEN '09' THEN 'Black'
    WHEN '69' THEN 'Navy'
    ELSE 'Unknown Color'
  END as color
FROM l_nvr_ec_returns) color,
    SUM(return_qty) number_of_returns,
    number_of_returns/  
    (SELECT
    SUM(return_qty)
FROM 
    l_nvr_ec_returns
WHERE 
    return_created_date BETWEEN '2019-10-01' AND '2019-10-31'
    AND return_status NOT IN ('Cancelled', 'cancelled')
    AND return_qty > 0
    AND return_reason_desc = 'Color Not As Expected'
) return_rate



FROM 
    l_nvr_ec_returns
WHERE 
    return_created_date BETWEEN '2019-10-01' AND '2019-10-31'
    AND return_status NOT IN ('Cancelled', 'cancelled')
    AND return_qty > 0
    AND return_reason_desc = 'Color Not As Expected'

GROUP BY item_name, color
ORDER BY color

Solution

  • Carefully edit your query to remove the characters obscured by red, leaving a trailing comma:

    enter image description here

    CASE is an expression that is supposed to be used in the select list, to convert a single SKU to a single color, per row of l_nvr_ec_returns