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

merger column value of duplicate records

I have a query that returns records to me, the record is duplicated when it finds more than 1 value

i would like to merge duplicate records but i dont know how

I’ve heard about aggregate functions, should I go in this direction? I don’t really know how to do it

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

SELECT distinct LCM.eFolderID, LCM.ContractNumber
        ,(bU.FirstName + ' ' + bU.Surname) as WeryfikacjaUmowa
        --,COUNT(LCM.ContractNumber)
  FROM Metastorm.dbo.LeasingContractsMap LCM with(nolock)
  left join Metastorm.dbo.eEvent eE with(nolock) on eE.eFolderID = LCM.EFOLDERID and eE.eActionName = 'AssignToContractVerificationAct'
  left join Metastorm.dbo.bpm_Users bU with(nolock) on bU.eUserName = eE.eUserName
  where LCM.ContractNumber not like '%purged%'
  and LCM.ContractNumber = '37/1276/20'

result

enter image description here

result i want

enter image description here

>Solution :

You can use string_agg to aggregate string columns when using group by in sql server.

Try this:

with u as
(SELECT distinct LCM.eFolderID, LCM.ContractNumber
            ,(bU.FirstName + ' ' + bU.Surname) as WeryfikacjaUmowa
            --,COUNT(LCM.ContractNumber)
      FROM Metastorm.dbo.LeasingContractsMap LCM with(nolock)
      left join Metastorm.dbo.eEvent eE with(nolock) on eE.eFolderID = LCM.EFOLDERID and eE.eActionName = 'AssignToContractVerificationAct'
      left join Metastorm.dbo.bpm_Users bU with(nolock) on bU.eUserName = eE.eUserName
      where LCM.ContractNumber not like '%purged%'
      and LCM.ContractNumber = '37/1276/20')
select eFolderID, ContractNumber, string_agg(WeryfikacjaUmowa, ' ') as WeryfikacjaUmowa
from u 
group by eFolderID, ContractNumber
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