【问题标题】:Count how many customers have 1 service , how many customers have 2 services计算有多少客户有 1 项服务,有多少客户有 2 项服务
【发布时间】:2014-05-13 08:34:17
【问题描述】:

我有这张桌子

id_cliente | serv_cuidpess | serv_ali | serv_hab | serv_roupa

125       | 1              | 2        | 1        | 1  
126       | 2              | 1        | 2        | 1

这意味着 id_cliente = customerID 和其他 serv_ = services 其中 1 = 客户拥有该服务,2 = 他没有。

我想要实现的是计算有多少客户有 1 项服务,有多少客户有 2 项服务,有多少客户有 3 项服务等等。我正在使用 PHP / MySQL

【问题讨论】:

  • 能否请您以易于可视化的表格形式概述预期结果。
  • 我的预期结果是 10 个客户有 1 个服务,5 个客户有 2 个服务..等等。 (注意 2 表示没有服务)。
  • 不清楚您能否从上表中详细说明您对输出的期望。
  • 输出应该显示有多少客户有 1 个服务,有多少客户有 2 个服务,有多少 cients 有 3 个服务。例如,在上面的数据中,我输入的输出应该是:1 个客户端有 3 个服务,1 个客户端有 2 个服务(MySQL 表中的值 1 表示他有该服务,2 表示他没有)。

标签: php mysql select count


【解决方案1】:

不确定您到底在寻找什么,有 2 个案例

当每个客户 = 1 时,获取所使用的总服务。

当它 = 1 时获取每项服务的客户数

通过以上内容,您可以尝试以下查询,但最好在问题中提供有关预期输出的更多信息。

对于第一种情况,您可以这样做

select
s.id_cliente,
sum(s1.ser1+s1.ser2+s1.ser3+s1.ser4) as tot_services
from services s
inner join
(
  select
  id_cliente,
  case  when serv_cuidpess = 1 then 1 else 0 end as ser1,
  case  when serv_ali = 1 then 1 else 0 end as ser2,
  case  when serv_hab = 1 then 1 else 0 end as ser3,
  case  when serv_roupa = 1 then 1 else 0 end  as ser4
  from services 


)s1 on s1.id_cliente = s.id_cliente
group by s.id_cliente;

对于第二种情况,您可以这样做

select
serv1_client_count,
serv2_client_count,
serv3_client_count,
serv4_client_count
from services s
inner join
(
  select 
  s1.id_cliente,
  sum(s2.ser1) as serv1_client_count,
  sum(s2.ser2) as serv2_client_count,
  sum(s2.ser3) as serv3_client_count,
  sum(s2.ser4) as serv4_client_count
  from services s1
  inner join
  (
    select
    id_cliente,
    case  when serv_cuidpess = 1 then 1 else 0 end as ser1,
    case  when serv_ali = 1 then 1 else 0 end as ser2,
    case  when serv_hab = 1 then 1 else 0 end as ser3,
    case  when serv_roupa = 1 then 1 else 0 end  as ser4
    from services 
  )s2 on  s2.id_cliente = s1.id_cliente
)t on t.id_cliente = s.id_cliente

DEMO

更新

来自评论

输出应该显示有多少有 1 服务而不是服务 1.. 如何 许多人有 2 项服务而不是 2 项服务

你可以这样做

select
sum(client_count) as client_count,
serv_count
from services s
inner join
(
  select 
  s1.id_cliente,
  1 as client_count,
  sum(s2.ser1+s2.ser2+s2.ser3+s2.ser4) as serv_count
  from services s1
  inner join
  (
    select
    id_cliente,
    case  when serv_cuidpess = 1 then 1 else 0 end as ser1,
    case  when serv_ali = 1 then 1 else 0 end as ser2,
    case  when serv_hab = 1 then 1 else 0 end as ser3,
    case  when serv_roupa = 1 then 1 else 0 end  as ser4
    from services 
  )s2 on  s2.id_cliente = s1.id_cliente
  group by s1.id_cliente
)t on t.id_cliente = s.id_cliente
group by serv_count

DEMO

【讨论】:

  • 是的,这是我期待的第一个案例。但我希望输出 COUNT 有多少客户有 1 项服务,有多少客户有 2 项服务,有多少客户有 3 项服务。在您的第一种情况下,输出是每个客户有多少服务。现在我想 GROUP(?) 拥有 1 项服务的客户,拥有 2 项服务的客户等等......
  • 这就是我的第二个查询,它的作用完全相同,即计算客户有多少服务1,有多少服务2等等。
  • 我很抱歉没有正确解释自己...输出应该显示有多少有 1 服务而不是服务 1.. 有多少有 2 服务而不是服务 2..
  • 这正是我想要的!太感谢了。我没有足够的声望来投票给你。但我会得到它,并让她投票给你!再次感谢你。
【解决方案2】:

既然你提到了 serv_ 作为进程,我假设所有这些(serv_cuidpess + serv_ali + serv_hab + serv_roupa ) 形成进程。

要获得总和,您可以使用+ 运算符直接添加列HAVINGclause 用于过滤虚拟列,即 TotalProcess

试试下面的查询

 select COUNT(id_cliente) AS NoOfUsers,(serv_cuidpess + serv_ali + serv_hab + serv_roupa ) as TotalProcess from PROCESSTABLE GROUP BY TotalProcess HAVING TotalProcess > 0;

【讨论】:

  • 对不起,我不明白你所说的进程是什么意思。 serv_cuidpess , serv_hab 是 MySQL 表 (int) 中的简单列。例如,拥有洗衣房 (serv_roupa) 的客户在该列中有 1,如果他没有 2
猜你喜欢
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多