当 else with partition by 在 redshift 查询中不起作用时
When else with partition by isn't working in redshift queries
我想从 TAG_SALES_by_month 中排除标签的类别 sub_tag1、sub_tag2 和 sub_tag3,但其余的无论我在 where 条件中提到的是什么计入计数。我无法达到预期的效果 result.can 谁能帮助我达到同样的效果,我将不胜感激。
select o.tag,
o.SOME, o.THING, o.ILIKE, o.date, c.THE, c.MOST,
date_part(month, o.date) as Month,
date_part(day, o.date) as day,
count(o.id) over (partition by day, CUST_Id) as SALE_NO,
count(o.id) over (partition by Month, CUST_Id) as SALE_NO_by_month,
count(case when (tag <> 'sub_tag1' AND tag <> 'sub_tag2' AND tag <> 'sub_tag3') then o.id else 0 END) over (partition by Month, CUST_Id) as TAG_SALES_by_month,
c.id as CUST_Id
from order_info o
left join config c on o.SOME = c.SOME
where date >= '05/01/2021' AND tag in ('sub_tag1', 'sub_tag2', 'sub_tag3', 'sub_tag4', 'sub_tag5',
'sub_tag6') AND ILIKE = 'JACK'
group by o.tag, o.SOME, o.THING, o.ILIKE, o.date, c.THE, c.MOST, CUST_Id, o.id
order by date
根据评论,这里的问题是 COUNT
将 return 1 用于任何值,它计算 存在与不存在 value/row.
因此 COUNT(CASE WHEN... ELSE 0...)
仍会在 ELSE
条件下计数 1,因为 0 是一个存在的值。
解决方案是使用ELSE NULL
或省略ELSE
子句,默认为NULL
,因为NULL
不会被计算在内。
我想从 TAG_SALES_by_month 中排除标签的类别 sub_tag1、sub_tag2 和 sub_tag3,但其余的无论我在 where 条件中提到的是什么计入计数。我无法达到预期的效果 result.can 谁能帮助我达到同样的效果,我将不胜感激。
select o.tag,
o.SOME, o.THING, o.ILIKE, o.date, c.THE, c.MOST,
date_part(month, o.date) as Month,
date_part(day, o.date) as day,
count(o.id) over (partition by day, CUST_Id) as SALE_NO,
count(o.id) over (partition by Month, CUST_Id) as SALE_NO_by_month,
count(case when (tag <> 'sub_tag1' AND tag <> 'sub_tag2' AND tag <> 'sub_tag3') then o.id else 0 END) over (partition by Month, CUST_Id) as TAG_SALES_by_month,
c.id as CUST_Id
from order_info o
left join config c on o.SOME = c.SOME
where date >= '05/01/2021' AND tag in ('sub_tag1', 'sub_tag2', 'sub_tag3', 'sub_tag4', 'sub_tag5',
'sub_tag6') AND ILIKE = 'JACK'
group by o.tag, o.SOME, o.THING, o.ILIKE, o.date, c.THE, c.MOST, CUST_Id, o.id
order by date
根据评论,这里的问题是 COUNT
将 return 1 用于任何值,它计算 存在与不存在 value/row.
因此 COUNT(CASE WHEN... ELSE 0...)
仍会在 ELSE
条件下计数 1,因为 0 是一个存在的值。
解决方案是使用ELSE NULL
或省略ELSE
子句,默认为NULL
,因为NULL
不会被计算在内。