0

我进行了以下查询:

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中不重复计数的语句,但我无法实现类似于我在上面向您展示的查询中的内容。

4

2 回答 2

0

你可以使用 COUNT(DISTINCT expr,[expr...])

http://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html#function_count-distinct

于 2014-10-30T23:09:02.220 回答
0

重复从哪里来?

我认为它们是由您的一个连接引起的。您可以在此联接中添加附加子句以摆脱它们。例子:

SELECT *
FROM sf_visit_stock AS vs
JOIN sf_visit AS v ON vs.sk_vi_id_visit = v.id_visit AND v.somevalue = 1

也可以在 WHERE 子句中去掉它们。

于 2020-01-11T11:50:16.727 回答