简体   繁体   English

具有 NULL 值的 CASE WHEN 表达式 (Hiveql)

[英]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)预期的 output :(当然是当前的#数量) 在此处输入图像描述

I have 3 CASE WHEN statements that are supposed to represent the # of sold products in the last 3, 6 and 12 months.我有 3 个 CASE WHEN 语句应该代表过去 3、6 和 12 个月内售出的产品数量。 However, some clients appear with NULL values even though I am aware we sold them products 22 days ago (example).然而,一些客户出现 NULL 值,即使我知道我们在 22 天前向他们出售了产品(示例)。

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.如果不正确,您可以共享一些示例数据进行检查,我认为您应该按项目关注您的分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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