简体   繁体   中英

CASE WHEN expressions with NULL values (Hiveql)

I have the following query:

SELECT
      rv.country
    , coalesce(rv.client_id, client_id2) as AccountID
    , coalesce(c.name, rv.client_id2)  as AccountName
    , rv.prod_type as ProdType,
    CASE WHEN cs.prod_type IS NULL THEN 'Unknown' ELSE cs.prod_type END as ProdType1,
    ROUND(-SUM(CASE WHEN DATEDIFF(current_date(), rv.date_time) < 90 THEN rv.sold_prods END)) as Last3Months,
    ROUND(-SUM(CASE WHEN DATEDIFF(current_date(), rv.date_time) < 180 THEN rv.sold_prods END)) as Last6Months,
    ROUND(-SUM(CASE WHEN DATEDIFF(current_date(), rv.date_time) < 360 THEN rv.sold_prods END)) as Last12Months
FROM
    commercial.sold_products rv
LEFT JOIN commercial.customer c
ON c.client_id2 = rv.client_id

LEFT JOIN 
    commercial.customer_description cs
ON
    cs.clientid = c.client_id2
    
WHERE rv.country NOT IN ('US', 'JP')
AND cs.prod_type = rv.prod_type
GROUP BY
      rv.country
    , rv.client_id
    , rv.client_id2
    , cs.prod_type
    , c.name
    , rv.prod_type;

Expected output: (With current # amount of values, of course) 在此处输入图像描述

I have 3 CASE WHEN statements that are supposed to represent the # of sold products in the last 3, 6 and 12 months. However, some clients appear with NULL values even though I am aware we sold them products 22 days ago (example).

I'm not sure what's wrong with this query, so if you notice something please let me know!

Also! Ignore the columns/tables names, I changed them because of company privacy issues. Thank you:)

Change your query like this:

SELECT
      rv.country
    , coalesce(rv.client_id, client_id2) as AccountID
    , coalesce(c.name, rv.client_id2)  as AccountName
    , rv.prod_type as ProdType,
    CASE WHEN cs.prod_type IS NULL THEN 'Unknown' ELSE cs.prod_type END as ProdType1,
    ROUND(-SUM(CASE WHEN DATEDIFF(current_date(), rv.date_time) < 90 THEN rv.sold_prods END)) as Last3Months,
    ROUND(-SUM(CASE WHEN DATEDIFF(current_date(), rv.date_time) < 180 THEN rv.sold_prods END)) as Last6Months,
    ROUND(-SUM(CASE WHEN DATEDIFF(current_date(), rv.date_time) < 360 THEN rv.sold_prods END)) as Last12Months
FROM
    commercial.sold_products rv
LEFT JOIN commercial.customer c
ON c.client_id2 = rv.client_id

LEFT JOIN 
    commercial.customer_description cs
ON
    cs.clientid = c.client_id2 AND cs.prod_type = rv.prod_type
    
WHERE rv.country NOT IN ('US', 'JP')
GROUP BY
      rv.country
    , coalesce(rv.client_id, rv.client_id2) 
    , rv.client_id2
    , cs.prod_type
    , coalesce(c.name, rv.client_id2)
    , rv.prod_type;

If not correct you can share some sample data to check and I think you should focus on your group by items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM