Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compare multiple differences in SQL query

Currently I use this query to show the differences:

SELECT 
    CASE
        WHEN PaymentMethodCRM <> PaymentMethodBOU THEN 'Payement Method CRM / BOU' 
        WHEN PaymentMethodCRM <> PaymentMethodFicheERP THEN 'Payement Method CRM / ERP'
        WHEN PaymentDeadlineCRM <> PaymentDeadlineBOU THEN 'Payment Deadline CRM / BOU'
        WHEN PaymentDeadlineCRM <> PaymentDeadlineFicheERP THEN 'Payment Deadline CRM / ERP'
        WHEN EditModeBOU <> EditModeContratERP THEN 'Edit Mode BOU / ERP'
        WHEN LTRIM(RTRIM(CompanyNameCRM)) <> LTRIM(RTRIM(CompanyNameBOU)) THEN 'Company Name CRM / BOU'
        WHEN LTRIM(RTRIM(CompanyNameBOU)) <> LTRIM(RTRIM(CAST(CompanyNameFicheERP AS NVARCHAR(50)))) THEN 'Company Name BOU / ERP'
        ELSE ''
    END AS Verification
FROM 
    Temp

But how to do if there are multiple differences?

My goal it’s to show it like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Payement Method CRM / BOU ; Payement Method CRM / ERP ; Payment Deadline CRM / ERP

I don’t want to have twenty lines of code to test each case

>Solution :

In SQL-Server 2016; You can do it using STUFF and FOR XML PATH.

SELECT STUFF((SELECT '; ' + DIFFERENCE 
              FROM (
                  SELECT 
                      CASE
                          WHEN PaymentMethodCRM <> PaymentMethodBOU THEN 'Payement Method CRM / BOU' 
                          WHEN PaymentMethodCRM <> PaymentMethodFicheERP THEN 'Payement Method CRM / ERP'
                          WHEN PaymentDeadlineCRM <> PaymentDeadlineBOU THEN 'Payment Deadline CRM / BOU'
                          WHEN PaymentDeadlineCRM <> PaymentDeadlineFicheERP THEN 'Payment Deadline CRM / ERP'
                          WHEN EditModeBOU <> EditModeContratERP THEN 'Edit Mode BOU / ERP'
                          WHEN LTRIM(RTRIM(CompanyNameCRM)) <> LTRIM(RTRIM(CompanyNameBOU)) THEN 'Company Name CRM / BOU'
                          WHEN LTRIM(RTRIM(CompanyNameBOU)) <> LTRIM(RTRIM(CAST(CompanyNameFicheERP AS NVARCHAR(50)))) THEN 'Company Name BOU / ERP'
                          ELSE ''
                      END AS DIFFERENCE
                  FROM 
                      Temp
                  ) AS DIFFERENCES_TABLE
              WHERE DIFFERENCE <> ''
              FOR XML PATH('')), 1, 2, '') AS DIFFERENCES;

In SQL-Server 2017; You can use STRING_AGG for the same.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading