Уже используется функция IS NULL, но все равно возвращает значение NULL - PullRequest
0 голосов
/ 30 апреля 2019

Я создаю запрос, используя CASE WHEN, и я заявляю в 1 CASE, используя функцию IS NULL. Но результат запроса все еще возвращает значения NULL.

Ниже мой запрос

SELECT 
    CASE
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Customer membatalkan pesanan' THEN 'Customer canceling order'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Deskripsi dalam order tidak sama' THEN 'Description not match'        
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Barang rusak' THEN 'Item defective'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Salah satu barang rusak' THEN 'Partially defective'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Salah satu stok tidak tersedia' THEN 'Partially not available'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Stok tidak tersedia' THEN 'Stock not available'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Order tidak bisa di scan' THEN 'Cannot scan order'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Order belum dibayar' THEN 'Order not paid'
        WHEN b.failure_detail IS NULL THEN 'Fulfillment Timeout'
    END AS 'Cancellation Remarks',
    COUNT(*) as 'Total'
FROM
    deli_order A
INNER JOIN
    deli_order_store B ON B.deli_order_id = A.id
WHERE
    (DATE(A.plat_create_time) BETWEEN '2019-04-21' AND '2019-04-27') AND 
    (A.status = 8) AND 
    (A.pay_state = 1) AND 
    NOT EXISTS (
        SELECT 1
        FROM deli_order AS B
        WHERE B.order_code = A.order_code
        AND B.code = A.code
        AND B.status = 6)
GROUP BY
    CASE
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Customer membatalkan pesanan' THEN 'Customer canceling order'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Deskripsi dalam order tidak sama' THEN 'Description not match'        
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Barang rusak' THEN 'Item defective'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Salah satu barang rusak' THEN 'Partially defective'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Salah satu stok tidak tersedia' THEN 'Partially not available'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Stok tidak tersedia' THEN 'Stock not available'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Order tidak bisa di scan' THEN 'Cannot scan order'
        WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Order belum dibayar' THEN 'Order not paid'
        WHEN b.failure_detail IS NULL THEN 'Fulfillment Timeout'
    END;

и это результат моего запроса

Cancellation Remarks | Total
NULL                    35
Description not match    1
Fulfillment Timeout     19
Item defective           6
Stock not available     13

Что-то не так с моим запросом?

Ответы [ 2 ]

1 голос
/ 30 апреля 2019

A CASE выражение оценивается как NULL, если ни одна из ветвей не совпадает. Добавьте ветку ELSE к вашему CASE, чтобы учесть, что это что-то совершенно другое, и ваши NULL s должны исчезнуть.

CASE
    WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Customer membatalkan pesanan' THEN 'Customer canceling order'
    WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Deskripsi dalam order tidak sama' THEN 'Description not match'        
    WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Barang rusak' THEN 'Item defective'
    WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Salah satu barang rusak' THEN 'Partially defective'
    WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Salah satu stok tidak tersedia' THEN 'Partially not available'
    WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Stok tidak tersedia' THEN 'Stock not available'
    WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Order tidak bisa di scan' THEN 'Cannot scan order'
    WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(B.failure_detail, '"remark":"', -1), '"', 1) = 'Order belum dibayar' THEN 'Order not paid'
    WHEN b.failure_detail IS NULL THEN 'Fulfillment Timeout'
    ELSE 'Something completely different'
END
0 голосов
/ 30 апреля 2019

Просто чтобы расширить @sticky bit answer на примере

MariaDB [sandbox]> select id,password from users;
+------+----------+
| id   | password |
+------+----------+
|    1 | NULL     |
|    2 | NULL     |
|    3 | zzz      |
|    4 | NULL     |
|  999 | NULL     |
| 1000 | NULL     |
| 1001 | NULL     |
+------+----------+
7 rows in set (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> select case
    ->  when password = 'abc' then 'found'
    ->   when password is null then 'notfound'
    ->  end as pfound,
    -> count(*)
    -> from users
    -> group by
    -> case
    ->  when password = 'abc' then 'found'
    ->   when password is null then 'notfound'
    ->    end;
+----------+----------+
| pfound   | count(*) |
+----------+----------+
| NULL     |        1 |
| notfound |        6 |
+----------+----------+
2 rows in set (0.00 sec)
...