简体   繁体   English

MySQL GROUP BY 与特定条件

[英]MySQL GROUP BY with specific condition

I'm using MySQL 5.5 .我正在使用MySQL 5.5 This is my query, the first part shows info about the company and it's vehicles, and the second part shows only company info.这是我的查询,第一部分显示有关公司及其车辆的信息,第二部分仅显示公司信息。

I did the UNION part because some smallClients doesn't have any vehicles, and since at the first part it join with vehicles, those ones didn't appear.我做了UNION部分,因为一些 smallClients 没有任何车辆,并且由于在第一部分它join车辆连接,那些没有出现。

What is the problem, some registers from the first and second part have the same smallClient, and since at the vehicles part I'm already showing company info, I would like to group by, only if they are the same idClient and the same department, and numberPlate is not null (if it's null it's ok, I just want to remove those registers which are redundant cause info it's already showing in vehicle part).有什么问题,第一部分和第二部分的一些寄存器具有相同的 smallClient,并且由于在车辆部分我已经显示了公司信息,我想分组,前提是它们是相同的 idClient 和相同的部门,并且 numberPlate 不是 null (如果它是 null 没关系,我只想删除那些冗余原因信息的寄存器,它已经显示在车辆部件中)。

Some companies can have more than one number plate too, and they cannot be grouped, cause each number plate has it's own documentation (this query is smaller than the real one).有些公司也可以有多个车牌,并且不能分组,因为每个车牌都有自己的文档(这个查询比真实的小)。

What results looks like now:现在的结果如下:

idClient  company          department    numberplate
3345      TONY FERGUSON    Commercial    null
3345      TONY FERGUSON    Financial     null
3345      TONY FERGUSON    Commercial    8453JVD

In this example, the first register should be gone, because this client has a numberPlate, and the register with the same department (first one), is redundant cause third registers shows the same.在这个例子中,第一个寄存器应该没有了,因为这个客户端有一个 numberPlate,并且同一个部门的寄存器(第一个)是多余的,因为第三个寄存器显示相同。

Another example:另一个例子:

idClient  company          department               numberplate
1267      TERRY SL         Distribution - France    null
1267      TERRY SL         Distribución - France    6381JHZ
1267      TERRY SL         Forwarding UK            null
1267      TERRY SL         Forwarding UK            6381JHZ

In this case, first and third register should dissapear, hope I explained all propertly.在这种情况下,第一个和第三个寄存器应该消失,希望我解释得当。

This is my query:这是我的查询:

select  
    idClient as "IdClient",
    Company  as "Company",         
    if (Department is null, "", Department) as "Department",
    Numberplate,               
 from      
    (select
         sc.idClient as idclient,
         sc.businessname as company,     
         d.name as department,          
         v.numberplate as numberPlate,               
    from entity_type et
         join bigClient bc on bc.idClient=et.idClient
         join smallClient sc on sc.idclient=bc.idClientAssociated
         join clientVehicle cv on cv.idClientVehicle=sc.idClientVehicle
         join vehicle v on v.idVehicle=cv.idVehicle    
         join active_vehicle av on av.idEntityType=et.idEntityType and av.idClientVehicle=cv.idClientVehicle 
         left join department d on d.idDepartment=et.idDepartment   
         where bc.idClient=1234   
    UNION
    select 
         c.idClient as idclient,
         c.businessname as empresavehicle,      
         cd.name as departament,      
         null as matricula,          
        from entity_type et
         join bigClient bc on bc.idClient=et.idClient
         join smallClient sc on sc.idclient=bc.idClientAssociated
         join active_c ac on ac.idEntityType=et.idEntityType and ac.idBigClient=bc.idBigClient   
         left join department d on d.idDepartment=et.idDepartment        
         where bc.idClient=1234) t
    order by company,numberplate;

You can use the analytical function count as follows:您可以使用解析 function count如下:

select * from
(select  
    idClient as "IdClient",
    Company  as "Company",         
    if (Department is null, "", Department) as "Department",
    Numberplate,
    count(Numberplate) over (partition by idClient,company,department) as cnt
  from
    .... rest of your query ....
) t
where cnt = 0 or (cnt>0 and Numberplate is not null)

The @Popeye solution should to work in MySQL 8.o or greater, for MySQL 5.x I can to advice next: @Popeye 解决方案应该在 MySQL 8.o 或更高版本中工作,对于 MySQL 5.x 我可以建议下一个:

select  
    idclient as "IdClient",
    company  as "Company",         
    if (department is null, "", department) as "Department",
    group_concat(numberplate) as ,
 from      
    (select
         sc.idClient as idclient,
         sc.businessname as company,     
         d.name as department,          
         v.numberplate as numberplate,               
    from entity_type et
         join bigClient bc on bc.idClient=et.idClient
         join smallClient sc on sc.idclient=bc.idClientAssociated
         join clientVehicle cv on cv.idClientVehicle=sc.idClientVehicle
         join vehicle v on v.idVehicle=cv.idVehicle    
         join active_vehicle av on av.idEntityType=et.idEntityType and av.idClientVehicle=cv.idClientVehicle 
         left join department d on d.idDepartment=et.idDepartment   
         where bc.idClient=1234   
    UNION
    select 
         c.idClient as idclient,
         c.businessname as company,      
         cd.name as departament,      
         null as numberplate,          
        from entity_type et
         join bigClient bc on bc.idClient=et.idClient
         join smallClient sc on sc.idclient=bc.idClientAssociated
         join active_c ac on ac.idEntityType=et.idEntityType and ac.idBigClient=bc.idBigClient   
         left join department d on d.idDepartment=et.idDepartment        
         where bc.idClient=1234) t
    group by idClient, Company, Department
    order by company,numberplate;

this query should work with one limitation - in case different numberplate it return one concatinated string这个查询应该有一个限制 - 如果不同的车牌它返回一个连接字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM