How to change STRING_AGG to stuff xml to split feature value by pip?

ahmed salah 3,216 Reputation points
2022-09-14T15:24:10.227+00:00

I work on sql server 2019 . i can't write query below with stuff for xml .

so how to change STRING_AGG to stuff xml ?

query below take too much time so i need to try

with stuff for xml to reduce time cost .

query i try it

select  
  a.RecomendationId,  
  cast(STRING_AGG(cast(f1.FeatureValue as varchar(300)) ,'|')  WITHIN GROUP (ORDER BY f1.FeatureId ASC) as varchar(300)) AS DiffFeatures  
into ExtractReports.dbo.TechnologyOriginalFeaturesEqual  
from extractreports.dbo.partsrecomendationActive a   
inner join ExtractReports.dbo.TechnologyPlPartsFeaturValuesOrg f1  on f1.partid = a.OrignalPartId  
inner join [Technology].Receipe Ft  on ft.featureid = f1.featureid and ft.operatorid = 1  
group by a.RecomendationId  

ddl tables structures

create table ExtractReports.dbo.TechnologyPlPartsFeaturValuesOrg  
      (  
      ID int identity(1,1),  
      PartId int,  
      FeatureID int,  
      FeatureName varchar(200),  
      FeatureValue varchar(200)  
      )  
  
create table extractreports.dbo.partsrecomendationActive  
(  
RecomendationId int identity(1,1),  
OrignalPartId int  
  
)  
  
CREATE TABLE [Technology].[Receipe](  
    [ReceipeID] [int] IDENTITY(1,1) NOT NULL,  
    [FeatureID] [int] NULL,  
    [OperatorID] [int] NULL,  
PRIMARY KEY CLUSTERED   
(  
    [ReceipeID] ASC  
))  

expected result
241118-image.png

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,690 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,626 questions
{count} votes

Accepted answer
  1. LiHongMSFT-4306 26,791 Reputation points
    2022-09-15T07:27:41.173+00:00

    Hi @ahmed salah

    how to change STRING_AGG to stuff xml

    Check this:

    ;WITH CTE AS  
    (  
     select a.RecomendationId, cast(f1.FeatureValue as varchar(300)) AS FeatureValue,f1.featureid  
     from extractreports.dbo.partsrecomendationActive a   
     inner join ExtractReports.dbo.TechnologyPlPartsFeaturValuesOrg f1  on f1.partid = a.OrignalPartId  
     inner join [Technology].Receipe Ft  on ft.featureid = f1.featureid and ft.operatorid = 1  
    )  
    SELECT A.RecomendationId  
          ,STUFF((SELECT  '|' + FeatureValue FROM cte B where B.RecomendationId=A.RecomendationId  
                  ORDER BY FeatureId ASC FOR XML PATH('')),1,1,'') AS DiffFeatures  
    FROM CTE A    
    GROUP BY A.RecomendationId  
    

    Best regards,
    LiHong


2 additional answers

Sort by: Most helpful
  1. Michael Taylor 53,971 Reputation points
    2022-09-14T16:12:11.013+00:00

    Since it is a perf issue we're not going to be a lot of help since we don't have the large dataset to notice the perf issue. Have you run the profiler to determine where it is taking the most time?

    I notice in your query you're doing casting. You don't need any of that so remove them. This will help a little bit. Also verify your joins are using indexed (Or FK relationship) columns. Otherwise that is going to impact the performance.

       select  
          a.RecomendationId,  
          STRING_AGG(f1.FeatureValue ,'|') WITHIN GROUP (ORDER BY f1.FeatureId ASC) AS DiffFeatures  
        from @partsrecomendationActive a   
        inner join @TechnologyPlPartsFeaturValuesOrg f1  on f1.partid = a.OrignalPartId  
        inner join @Receipe Ft  on ft.featureid = f1.featureid and ft.operatorid = 1  
        group by a.RecomendationId  
    

    Note: I'm using a table variable so ignore the table names.

    I also think your STRING_AGG could probably be converted to a regular group by on a subselect (or join or CTE) and then have the higher level select STRING_AGG. But without testing on a large dataset then it would be hard to tell if it impacted the performance. The profiler will tell you that.


  2. Erland Sommarskog 110.4K Reputation points MVP
    2022-09-14T21:31:49.057+00:00

    query below take too much time so i need to try

    with stuff for xml to reduce time cost .

    The likelihood that the FOR XML klduge would be faster is about nil. The reason query is slow is elsewhere.

    Upload the query plan on www.pastetheplan.com, so we have something to work from.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.