I have this table

id_cliente | serv_cuidpess | serv_ali | serv_hab | serv_roupa

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

which means id_cliente = customerID , and the others serv_ = services where 1 = customer has that service , 2 = he hasn't.

What I'm trying to achieve is counting how many customers have 1 service , how many customers have 2 services , how many customers have 3 services and so on.. I'm using PHP / MySQL

有帮助吗?

解决方案

Not sure what exactly you are looking for, there 2 cases

Get the total services being taken when its = 1 per customer.

Get the count of customers per service when its = 1

With the above you can try the following query, however its always better to provide more information on the expected output in the question.

For the first case you can do something as

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;

For the 2nd case you can do something as

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

UPDATE

From the comment

The Output should show how many have 1 service not service 1.. How many have 2 services not service 2

Here is what you can do

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

其他提示

Since you have mentioned serv_ as processes , I assume that all these(serv_cuidpess + serv_ali + serv_hab + serv_roupa ) form as processes.

To have the sum you can directly add columns with + operator HAVINGclause is for filtering virtual columns , i.e TotalProcess

Try the below query

 select COUNT(id_cliente) AS NoOfUsers,(serv_cuidpess + serv_ali + serv_hab + serv_roupa ) as TotalProcess from PROCESSTABLE GROUP BY TotalProcess HAVING TotalProcess > 0;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top