为什么 SUM() 和 COUNT() 返回不同的值?
Why is SUM() and COUNT() returning different values?
我有两个几乎相同的查询,我试图了解它们返回不同结果的原因。我想生成一个 table 和一个 user_id
和一个 food_orders
列,显示每个用户订购了多少商品。这些查询产生此 table 但在某些行的 food_orders 列中计算出不同的结果。
我的问题是为什么会这样,我应该使用哪个?
下面是我的查询的简化版本。
查询 1(使用 COUNT):
WITH order_made AS (
SELECT restaurant_id,
count(
CASE
WHEN item LIKE '%_angus' then 1
WHEN item LIKE '%_organic' then 1
WHEN item LIKE '%_lean' then 1
ELSE 0
END)
AS burgers
FROM mcdonalds.specialist_orders
GROUP BY mcdonalds.specialist_orders.user_id
UNION ALL
SELECT restaurant_id,
COUNT(
CASE
WHEN item LIKE 'salad%' THEN 1
WHEN item LIKE 'tomato%' THEN 1
WHEN item LIKE 'potatoes%' THEN 1
ELSE 0
END)
AS vegetables
FROM public.bulk_orders
GROUP BY public.bulk_orders.user_id),
查询 2(使用 SUM):
WITH orders_made AS (
SELECT user_id, SUM(food_orders) AS food_orders
FROM (SELECT user_id,
CASE
WHEN item LIKE '%_angus' then 1
WHEN item LIKE '%_organic' then 1
WHEN item LIKE '%_lean' then 1
ELSE 0
END
AS food_orders
FROM mcdonalds.specialist_orders
UNION ALL
SELECT user_id,
CASE
WHEN item LIKE 'salad%' THEN 1
WHEN item LIKE 'tomato%' THEN 1
WHEN item LIKE 'potatoes%' THEN 1
ELSE 0
END
AS food_orders
FROM public.bulk_orders
GROUP BY user_id)
因为COUNT
函数在值不为NULL(包括0)时才计数,如果不想计数,需要让CASE WHEN
returnNULL
因此,如果您从 CASE WHEN
中删除 ELSE 0
或使用 ELSE NULL
代替,结果将与 SUM
相同
WITH order_made AS (
SELECT restaurant_id,
count(
CASE
WHEN item LIKE '%_angus' then 1
WHEN item LIKE '%_organic' then 1
WHEN item LIKE '%_lean' then 1
END)
AS burgers
FROM mcdonalds.specialist_orders
GROUP BY mcdonalds.specialist_orders.user_id
UNION ALL
SELECT restaurant_id,
COUNT(
CASE
WHEN item LIKE 'salad%' THEN 1
WHEN item LIKE 'tomato%' THEN 1
WHEN item LIKE 'potatoes%' THEN 1
END)
AS vegetables
FROM public.bulk_orders
GROUP BY public.bulk_orders.user_id),
我有两个几乎相同的查询,我试图了解它们返回不同结果的原因。我想生成一个 table 和一个 user_id
和一个 food_orders
列,显示每个用户订购了多少商品。这些查询产生此 table 但在某些行的 food_orders 列中计算出不同的结果。
我的问题是为什么会这样,我应该使用哪个?
下面是我的查询的简化版本。
查询 1(使用 COUNT):
WITH order_made AS (
SELECT restaurant_id,
count(
CASE
WHEN item LIKE '%_angus' then 1
WHEN item LIKE '%_organic' then 1
WHEN item LIKE '%_lean' then 1
ELSE 0
END)
AS burgers
FROM mcdonalds.specialist_orders
GROUP BY mcdonalds.specialist_orders.user_id
UNION ALL
SELECT restaurant_id,
COUNT(
CASE
WHEN item LIKE 'salad%' THEN 1
WHEN item LIKE 'tomato%' THEN 1
WHEN item LIKE 'potatoes%' THEN 1
ELSE 0
END)
AS vegetables
FROM public.bulk_orders
GROUP BY public.bulk_orders.user_id),
查询 2(使用 SUM):
WITH orders_made AS (
SELECT user_id, SUM(food_orders) AS food_orders
FROM (SELECT user_id,
CASE
WHEN item LIKE '%_angus' then 1
WHEN item LIKE '%_organic' then 1
WHEN item LIKE '%_lean' then 1
ELSE 0
END
AS food_orders
FROM mcdonalds.specialist_orders
UNION ALL
SELECT user_id,
CASE
WHEN item LIKE 'salad%' THEN 1
WHEN item LIKE 'tomato%' THEN 1
WHEN item LIKE 'potatoes%' THEN 1
ELSE 0
END
AS food_orders
FROM public.bulk_orders
GROUP BY user_id)
因为COUNT
函数在值不为NULL(包括0)时才计数,如果不想计数,需要让CASE WHEN
returnNULL
因此,如果您从 CASE WHEN
中删除 ELSE 0
或使用 ELSE NULL
代替,结果将与 SUM
WITH order_made AS (
SELECT restaurant_id,
count(
CASE
WHEN item LIKE '%_angus' then 1
WHEN item LIKE '%_organic' then 1
WHEN item LIKE '%_lean' then 1
END)
AS burgers
FROM mcdonalds.specialist_orders
GROUP BY mcdonalds.specialist_orders.user_id
UNION ALL
SELECT restaurant_id,
COUNT(
CASE
WHEN item LIKE 'salad%' THEN 1
WHEN item LIKE 'tomato%' THEN 1
WHEN item LIKE 'potatoes%' THEN 1
END)
AS vegetables
FROM public.bulk_orders
GROUP BY public.bulk_orders.user_id),