Home » Php » PHP MySQL Group by multiple conditions in a unique situation

PHP MySQL Group by multiple conditions in a unique situation

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m working with data managed by another department and I’m getting duplicates.

There are 3 different unique identifies:

Order Number
Company Name
Company Entity

Sometimes its the same company name but a different order number.

Sometimes its the same company name but a different company entity.

Sometimes its the same order number but different company name.

Sometimes its the same order number but different company entity.

Sometimes its the same company entity but different order number.

Sometimes its the same company entity but different company name.

Sometimes the company names are abbreviated like Apple, Apple Inc, Apple NYC, Etc.

Below I have a table with example data:

CompanyName      CompanyEntity   OrderNumber   
Apple            123             555123            
Apple            123             555123         
Telsa            888             444444         
Telsa            777             222222         
Apple NYC        103             544523       
Stack            222             511523                  
Stack            222             144523       

I need to group by the company name or company entity or order number.

I tried:

 $query = "SELECT
 nextgenorders2.nextgenorder_ordernumber,
 nextgenorders2.nextgenorder_serial,
 nextgenorders2.nextgenorder_company_entity,
 nextgenorders2.nextgenorder_companyname,
 nextgenorders2.nextgenorder_deliverydate,
 FROM nextgenorders2
 WHERE nextgenorders2.nextgenorder_deliverydate='2020-11-11' 
 GROUP BY nextgenorders2.nextgenorder_companyname,
          nextgenorders2.nextgenorder_company_entity, 
          nextgenorders2.nextgenorder_ordernumber
 ";

That query produces:

CompanyName      CompanyEntity   OrderNumber   
Apple            123             555123            
Telsa            888             444444         
Telsa            777             222222         
Apple NYC        103             544523       
Stack            222             511523                  

If the row has the same company name, company entity and order number it’s put into a group.

If the row doesn’t have the same company entity, company name and order number it’s not put into a group.

I want the query to produce this:

CompanyName      CompanyEntity   OrderNumber   
Apple            123             555123                
Telsa            888             444444                           
Stack            222             144523       

but I’ll take this because of the multiple company name abbrievations:

CompanyName      CompanyEntity   OrderNumber   
Apple            123             555123                    
Telsa            777             222222         
Apple NYC        103             544523       
Stack            222             511523                  

Thank you for your help.

How to&Answers:

You can do something like this

But for every column you have do decide what to do with it.
Like MIN(n2.nextgenorder_company_entity), but if you want to keep all you can use GROUP_CONCAT for example

CREATE TABLE nextgenorders2
    (`nextgenorder_companyname` varchar(9), `nextgenorder_company_entity` int, `nextgenorder_ordernumber` int, nextgenorder_deliverydate date)
;

INSERT INTO nextgenorders2
    (`nextgenorder_companyname`, `nextgenorder_company_entity`, `nextgenorder_ordernumber`,nextgenorder_deliverydate)
VALUES
    ('Apple', 123, 555123,'2020-11-11'),
    ('Apple', 123, 555123,'2020-11-11'),
    ('Telsa', 888, 444444,'2020-11-11'),
    ('Telsa', 777, 222222,'2020-11-11'),
    ('Apple NYC', 103, 544523,'2020-11-11'),
    ('Stack', 222, 511523,'2020-11-11'),
    ('Stack', 222, 144523,'2020-11-11')
;
✓

✓
SELECT
 GROUP_CONCAT(n2.nextgenorder_ordernumber),
 #n2.nextgenorder_serial,
 MIN(n2.nextgenorder_company_entity),
 n2.nextgenorder_companyname,
 n2.nextgenorder_deliverydate
 FROM 
    (SELECT REPLACE(`nextgenorder_companyname`,'Apple NYC','Apple') `nextgenorder_companyname`
    , `nextgenorder_company_entity`, `nextgenorder_ordernumber`,nextgenorder_deliverydate
    FROM nextgenorders2) n2
 WHERE n2.nextgenorder_deliverydate='2020-11-11' 
 GROUP BY n2.nextgenorder_companyname
          ,n2.nextgenorder_deliverydate
          #,n2.nextgenorder_ordernumber
GROUP_CONCAT(n2.nextgenorder_ordernumber) | MIN(n2.nextgenorder_company_entity) | nextgenorder_companyname | nextgenorder_deliverydate
:---------------------------------------- | ----------------------------------: | :----------------------- | :------------------------
555123,555123,544523                      |                                 103 | Apple                    | 2020-11-11               
511523,144523                             |                                 222 | Stack                    | 2020-11-11               
444444,222222                             |                                 777 | Telsa                    | 2020-11-11               

db<>fiddle here

Some more explanation

The heart of the query is

(SELECT REPLACE(`nextgenorder_companyname`,'Apple NYC','Apple') `nextgenorder_companyname`
, `nextgenorder_company_entity`, `nextgenorder_ordernumber`,nextgenorder_deliverydate
FROM nextgenorders2) n2

This Replaces the company names so that in this Case Apple NYC becomes Apple, this is somewhat time consuming so you should think about creating a column Mother company, but if there are only a few you can make this manually like i did.

The Group By is reduced to only company name , because that is our main target, to get all Information on that, delivery date is there , because usually you look for the whole week, so it is already group by if you decide to look at two or more dates.

The select is only an example, because is don’t know, what you are searching for an your query didn’t give me any clue.

But the base is for every Column, that is not in the Group By clause , you need a Aggregation Function , so that the developer has to decide for every column what data he wants to see.

In my case i decided to see all order numbers if you need only the number of orders you take COUNT.

The entity isn’t in the Group by, because i don’t see why you need any entity at all if you only want to look at company’s as a whole, the min was only because you took for Tesla 777 and not 888, but i don’t know what you need it for