我进行了以下查询:
select ba_brand, ceil(sum((sk_front + sk_depth + sk_warehouse + (sk_exhibition IS NOT NULL))) / count(distinct(vi_pdv_id_pdv))) as 'inventory',
count(vi_pdv_id_pdv)
from sf_visit_stock
join sf_visit on sk_vi_id_visit = id_visit
join sf_pdv on vi_pdv_id_pdv = id_pdv
join sf_format on pdv_fo_id_format = id_format
join sf_group on fo_gr_id_group = id_group
join sf_channel on gr_ch_id_channel = id_channel
join sf_product on sk_pd_id_product = id_product
join sf_family on pd_fa_id_family = id_family
join sf_brand on fa_ba_id_brand = id_brand
where (CASE WHEN from_unixtime(vi_scheduled_start,'%Y-%m-%d') between '2014-10-01' and '2014-10-31' AND vi_vs_id_visit_status in (1,2,3,4,6) THEN 4 END)
and vi_pr_id_proyect = 5 and ba_rival = 0 and id_brand in (72,75,76,77,78,79,80)
and sf_channel.ch_channel not in ("CHAINS DIRECTS", "CHAINS INDIRECTS", "INDEPENDENTS", "SUPERMARKET")
group by ba_brand
换句话说,我正在做sum()
一些价值观:
sum((sk_front + sk_depth + sk_warehouse + (sk_exhibition IS NOT NULL))
从多行:
count(vi_pdv_id_pdv)
但是这些行中的 ID 是重复的,因此count(vi_pdv_id_pdv)
返回一个值,计算所有这些重复的 ID。我想获得那些vi_pdv_id_pdv
没有重复的价值。这里有一个小例子。通过此查询,我得到以下结果:
My Product | 32031 | 1098
在此示例中,1098 计数重复值。我想得到:
My Product | 32031 | 534
该计数没有重复值。我知道在我读过的这个答案group by
中不重复计数的语句,但我无法实现类似于我在上面向您展示的查询中的内容。