Tuesday, June 24, 2025

Getting multiple row data into single SQL column

Table data:

Id | Text | Description

1 | Test1 | Test description

1 | Test2 | Test description

1 | Test3 | Test description

2 | Test2 | Test description

3 | Test3 | Test description

Expected result: System should return "Test1;Test2;Test3" for data related to Id = '1'

Query (T-SQL)


Sample 1:

SELECT  t.LegalEntity, t.ITEMNUMBER,

       replace(STUFF((SELECT '; ' + CAST(t1.Text AS VARCHAR(255)) [text()]

         FROM TESTTABLE t1

         WHERE t1.ID = t.ITEMNUMBER and t1.LEGALENTITY = t.LEGALENTITY

         FOR XML PATH(''), TYPE)

        .value('.','NVARCHAR(255)'),1,2,''),' ','') AS ComputeDescription

FROM InventTable  t

where  t.ITEMNUMBER in ('1') and t.LEGALENTITY = 'usmf'

group by t.LegalEntity, t.ITEMNUMBER


Sample 2:

SELECT it.ITEMLE, it.ITEMNUMBER,

CAST (ISNULL((SELECT STRING_AGG(t1.Text,';') AS ProductLine 

 FROM TESTTABLE t1

where t1.ID = it.ITEMNUMBER and t1.LEGALENTITY = it.DataAreaId),'') as NVARCHAR(255)) AS ComputeDescription

from InventTable it

where it.DataAreaId = 'chhq'

and it.ITEMNUMBER in ('0000505','0000511','0000497','0000499')

No comments:

Post a Comment

Override custom dimension value on posting sales invoice in X++

 Requirement: 1. Override custom dimension value on posting sales invoice 2.  Ledger posting type as "Cost of goods, invoiced" ...