count() 函数 + derived table 和 Group - 使一切正常
count() function + derived table and Group - making it all work
有人可以帮我解释一下这个例子中 SUM 函数的位置吗?我正在尝试对 returned 列的数量进行字面计数,而不是每个 post id 的 meta_key 的数量。我想要一个数字,就像分组后有 5 个结果一样。
SELECT nmbr, post_id
, DeliveryDate
, DeliveryType
FROM ( SELECT nmbr, post_id
, MAX(CASE WHEN meta_key = 'value_1' THEN meta_value ELSE NULL END) as DeliveryDate
, MAX(CASE WHEN meta_key = 'value_2' THEN meta_value ELSE NULL END) as DeliveryType
FROM wp_postmeta
GROUP
BY post_id
) AS derived_table
WHERE DeliveryDate >= CURRENT_DATE
AND DeliveryType = 'delivery'
我已经尝试过了,但它没有计算或求和任何东西,HAVING 子句中有些东西导致 return 没有结果。如果我删除它,它会在 table 上显示所有结果,但不计算它们。
SELECT
post_id,
COUNT(*) AS cnt,
MAX(CASE WHEN meta_key = 'value_1' THEN meta_value END) AS DeliveryDate,
MAX(CASE WHEN meta_key = 'value_2' THEN meta_value END) AS DeliveryType
FROM wp_postmeta
GROUP BY
post_id
HAVING
DeliveryDate >= CURRENT_DATE AND
DeliveryType = 'delivery';
这是上面生成的图像
SELECT COUNT(*)
FROM (
SELECT nmbr, post_id
, DeliveryDate
, DeliveryType
FROM ( SELECT nmbr, post_id
, MAX(CASE WHEN meta_key = 'value_1' THEN meta_value ELSE NULL END) as DeliveryDate
, MAX(CASE WHEN meta_key = 'value_2' THEN meta_value ELSE NULL END) as DeliveryType
FROM wp_postmeta
GROUP
BY post_id
) AS derived_table
WHERE DeliveryDate >= CURRENT_DATE
AND DeliveryType = 'delivery'
) AS q
有人可以帮我解释一下这个例子中 SUM 函数的位置吗?我正在尝试对 returned 列的数量进行字面计数,而不是每个 post id 的 meta_key 的数量。我想要一个数字,就像分组后有 5 个结果一样。
SELECT nmbr, post_id
, DeliveryDate
, DeliveryType
FROM ( SELECT nmbr, post_id
, MAX(CASE WHEN meta_key = 'value_1' THEN meta_value ELSE NULL END) as DeliveryDate
, MAX(CASE WHEN meta_key = 'value_2' THEN meta_value ELSE NULL END) as DeliveryType
FROM wp_postmeta
GROUP
BY post_id
) AS derived_table
WHERE DeliveryDate >= CURRENT_DATE
AND DeliveryType = 'delivery'
我已经尝试过了,但它没有计算或求和任何东西,HAVING 子句中有些东西导致 return 没有结果。如果我删除它,它会在 table 上显示所有结果,但不计算它们。
SELECT
post_id,
COUNT(*) AS cnt,
MAX(CASE WHEN meta_key = 'value_1' THEN meta_value END) AS DeliveryDate,
MAX(CASE WHEN meta_key = 'value_2' THEN meta_value END) AS DeliveryType
FROM wp_postmeta
GROUP BY
post_id
HAVING
DeliveryDate >= CURRENT_DATE AND
DeliveryType = 'delivery';
这是上面生成的图像
SELECT COUNT(*)
FROM (
SELECT nmbr, post_id
, DeliveryDate
, DeliveryType
FROM ( SELECT nmbr, post_id
, MAX(CASE WHEN meta_key = 'value_1' THEN meta_value ELSE NULL END) as DeliveryDate
, MAX(CASE WHEN meta_key = 'value_2' THEN meta_value ELSE NULL END) as DeliveryType
FROM wp_postmeta
GROUP
BY post_id
) AS derived_table
WHERE DeliveryDate >= CURRENT_DATE
AND DeliveryType = 'delivery'
) AS q