1

Je souhaite concaténer des valeurs dans une colonne en fonction de l'ID.Comment concaténer le nom à l'aide de valeurs provenant de différentes colonnes dans SQL 2012

Existe-t-il un moyen de concaténer deux colonnes basées sur une autre colonne avec des instructions SQL?

Ma table se présente comme suit

customerID Name Values 
1   John 10apples 
1   John 20oranges 
1   John 30bananas 
2   Steve 15apples 
2   Steve 20oranges 
2   Steve 35bananas 

ma requête attendue devrait me donner quelque chose comme ça

customerID Name Values  FAKENAME 
1   John 10apples 10apples_20oranges_30bananas 
1   John 20oranges 
1   John 30bananas 
2   Steve 15apples 15apples_20oranges_35bananas 
2   Steve 20oranges 
2   Steve 35bananas 

essentiellement pour Ids distincts, il doit créer un nom de Famke en combinant les valeurs

+0

double possible de (https://stackoverflow.com/questions/10404348/sql -server-dynamic-pivot-query) –

+0

Copie possible de [Comment utiliser GROUP BY pour concaténer des chaînes dans SQL Server?] (https://stackoverflow.com/questions/273238/how-to-use-group-by- to-concatenate-strings-in-sql-server) –

Répondre

1

Vous pouvez l'utiliser.

DECLARE @Table TABLE (CustomerID INT, Name VARCHAR(20), [Values] VARCHAR(20)) 
INSERT INTO @Table 
VALUES 
(1 ,'John','10apples'), 
(1 ,'John','20oranges'), 
(1 ,'John','30bananas'), 
(2 ,'Steve','15apples'), 
(2 ,'Steve','20oranges'), 
(2 ,'steve','35bananas') 

;WITH CTE 
    AS (
    SELECT DISTINCT 
      CustomerID, 
      Name, 
      [Values], 
      STUFF(
       (
        SELECT '_'+[Values] 
        FROM @Table T2 
        WHERE T1.CustomerID = T2.CustomerID FOR XML PATH('') 
       ), 1, 1, '') FakeName 
    FROM @Table T1) 
    SELECT CustomerID, 
      Name, 
      [Values], 
      CASE 
       WHEN(ROW_NUMBER() OVER(PARTITION BY FakeName ORDER BY FakeName)) > 1 
       THEN '' 
       ELSE FakeName 
      END FakeName 
    FROM CTE; 

Résultat:

customerID Name Values  FAKENAME 
1   John 10apples 10apples_20oranges_30bananas 
1   John 20oranges 
1   John 30bananas 
2   Steve 15apples 15apples_20oranges_35bananas 
2   Steve 20oranges 
2   Steve 35bananas 
+0

Mise à jour ma réponse. –

0

Essayez ceci: [? SQL Server requête dynamique PIVOT]

;WITH cte0 AS(  
SELECT 1 AS CustomerID, 'John' AS [Name],'10apples' AS [Values] UNION 
SELECT 1 ,    'John'   ,'20oranges'   UNION 
SELECT 1 ,    'John'   ,'30bananas'   UNION 
SELECT 2 ,    'Steve'   ,'15strawberries'  UNION 
SELECT 2 ,    'Steve'   ,'25blueberries'  UNION 
SELECT 2 ,    'steve'   ,'35blackberries') 

,cte1 as(
SELECT customerid,[name], 
     STUFF((SELECT '_'+ [values] 
       FROM cte0 t1 
       WHERE t1.customerid = t2.customerid 
       FOR XML PATH ('')), 1, 1, '') AS FakeName 
FROM cte0 t2 
GROUP BY customerid,[name]), 

cte2 AS(
SELECT a.CustomerID,a.name,[values],fakename,Row_Number() OVER(PARTITION BY a.CustomerID ORDER BY a.CustomerID) AS RN 
FROM cte0 a 
    INNER JOIN cte1 b 
     ON a.CustomerID = b.customerid) 

SELECT CustomerID,[name],[values],CASE WHEN rn = 1 THEN FakeName ELSE '' END AS FakeName 
FROM cte2