Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

sql - MSSQL - GROUP_CONCAT

Here is the sample data :

IdProduit   Localisation    Qte_EnMain
4266864286880063006 E2-R40-B-T  13.00000
4266864286880063006 E2-R45-B-T  81.00000
4266864286880063007 E2-R45-C-T  17.00000
4266864286880063008 E2-R37-B-T  8.00000

And this is what i would like to have

IdProduit           AllLocalisation
4266864286880063006 E2-R40-B-T (13), E2-R45-B-T (81)
4266864286880063007 E2-R45-C-T (17)
4266864286880063008 E2-R37-B-T (8)

I watched all the examples of GROUP_CONCAT on the forum and I tried several tests.

I don't really understand STUFF().

Here is what i would like to do :

SELECT
  a.IdProduit,
  GROUP_CONCAT(
      CONCAT(b.Localisation, ' (', CAST(ROUND(a.Qte_EnMain, 0) AS NUMERIC(36, 0)), ')')
  ) AS AllLocation
FROM
  ogasys.INV_InventENTLoc a
  LEFT JOIN ogasys.INV_LocName b ON a.IdLoc = b.IdLoc
GROUP BY a.IdProduit, b.Localisation, a.Qte_EnMain

Now because GROUP_CONCAT is nto working with MSSQL this is the query i have created with all example on this forum.

SELECT
  DISTINCT
  a1.IdProduit,
  STUFF((SELECT DISTINCT '' + b2.Localisation
         FROM
           ogasys.INV_InventENTLoc a2
           LEFT JOIN ogasys.INV_LocName b2 ON a2.IdLoc = b2.IdLoc
         WHERE a2.IdLoc = a1.IdLoc
         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 0, '') data
FROM
  ogasys.INV_InventENTLoc a1
  LEFT JOIN ogasys.INV_LocName b1 ON a1.IdLoc = b1.IdLoc
ORDER BY a1.IdProduit

The query only return one localisation by row i don't understand how to make this query working.

EDIT:

Here is the solution for my situation :

SELECT
  a.IdProduit,
  STUFF(
      (SELECT ', ' + b2.Localisation + ' (' + CAST(CAST(ROUND(a2.Qte_EnMain, 0) AS NUMERIC(36, 0)) AS VARCHAR(32)) + ')'
       FROM ogasys.INV_InventENTLoc a2
         LEFT JOIN ogasys.INV_LocName b2 ON a2.IdLoc = b2.IdLoc
       WHERE a.IdProduit = a2.IdProduit
       FOR XML PATH (''))
      , 1, 1, '') AS AllLocalisation
FROM
  ogasys.INV_InventENTLoc a
  LEFT JOIN ogasys.INV_LocName b ON a.IdLoc = b.IdLoc
GROUP BY a.IdProduit
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

using STUFF

declare @table table (IdProduit varchar(100), Localisation varchar(50),  Qte_EnMain float)

insert into @table
values 
('4266864286880063006','E2-R40-B-T',  13.00000),
('4266864286880063006','E2-R45-B-T',  81.00000),
('4266864286880063007','E2-R45-C-T',  17.00000),
('4266864286880063008','E2-R37-B-T',  8.00000)


select IdProduit,
  STUFF (
        (SELECT   
                ',' + localisation + concat(' (',cast(qte_enMain as varchar(4)),') ')
        FROM @table t2
        where t2.IdProduit = t1.IdProduit
        FOR XML PATH('')), 1, 1, ''
    )
 from @table t1
group by
IdProduit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...