Объединить строки с разделителем точка с запятой после группировки - PullRequest
0 голосов
/ 28 мая 2020

Я хочу объединить свой результат разделителем точки с запятой, так что это мой запрос

SELECT 
   Concat(Year(info.[CreationDate]),'/',Trim('BS-' from info.ProjectN)) As 'AllProjectN'
  ,part.Designation  As 'AllDesignation' 
  ,cust.Name   As 'AllCustomer'
  ,prod.[Quantity] As 'AllQuantity'
  ,Case when prod.[Quantity] <=3 then prod.[Quantity] 
                   when prod.[Quantity] between 4 and 501 then 3
                   when prod.[Quantity] between 502 and 1201 then 5
                   when prod.[Quantity] between 1202 and 1801 then 8
                   when prod.[Quantity] between 1802 and 3200 then 13
                   else ' ' end as 'Echantillonnage'
  ,[GalvaQualityDailyFicheControle].[CreationDate]

FROM [dbo].[GalvaQualityDailyFicheControle]
Inner Join GalvaQualityDailyProduction prod on prod.id= [GalvaQualityDailyFicheControle].FK_idDailyProduction
Inner join GalvaParts part on part.id=prod.[FK_idPart]
Inner join ProjectInfoGalva info on info.id=part.IdProject
inner Join Customer cust on cust.ID=info.FK_Customer

Where Convert(Date,[GalvaQualityDailyFicheControle].[CreationDate]) = '05-27-2020' AND [GalvaQualityDailyFicheControle].FK_idNextProcess=13
Group By
   Concat(Year(info.[CreationDate]),'/',Trim('BS-' from info.ProjectN)) 
  ,part.Designation 
  ,cust.Name   
  ,prod.[Quantity] 
  ,[GalvaQualityDailyFicheControle].[CreationDate]

Я получаю этот результат enter image description here

I try with STRING_AGG I get one row but the data is duplicated

SELECT 
   STRING_AGG(Concat(Year(info.[CreationDate]),'/',Trim('BS-' from info.ProjectN))  , ' ;')As 'AllProjectN'
  ,STRING_AGG(part.Designation  , ' ;')As 'AllDesignation' 
  ,STRING_AGG(cust.Name   , ' ;')As 'AllCustomer'
  ,STRING_AGG(prod.[Quantity] , ' ;')As 'AllQuantity'
  ,STRING_AGG(Case when prod.[Quantity] 

This is the result I get введите описание изображения здесь

Как получить одну строку без дублирования данных?.

1 Ответ

0 голосов
/ 30 мая 2020
ALTER Proc [dbo].[RepDailyQualityGalvaFicheControleAll]
@Date date
AS
with myCTE 
(AllProjectN,AllDesignation,AllCustomer,AllQuantity,Echantillonnage,CreationDate)AS
(
SELECT 
   Concat(Year(info.[CreationDate]),'/',Trim('BS-' from info.ProjectN)) As 'AllProjectN'
  ,part.Designation  As 'AllDesignation' 
  ,cust.Name   As 'AllCustomer'
  ,prod.[Quantity] As 'AllQuantity'
  ,Case when prod.[Quantity] <=3 then prod.[Quantity] 
                   when prod.[Quantity] between 4 and 501 then 3
                   when prod.[Quantity] between 502 and 1201 then 5
                   when prod.[Quantity] between 1202 and 1801 then 8
                   when prod.[Quantity] between 1802 and 3200 then 13
                   else ' ' end as 'Echantillonnage'
  ,[GalvaQualityDailyFicheControle].[CreationDate]

FROM [dbo].[GalvaQualityDailyFicheControle]
Inner Join GalvaQualityDailyProduction prod on prod.id=[GalvaQualityDailyFicheControle].FK_idDailyProduction
Inner join GalvaParts part on part.id=prod.[FK_idPart]
Inner join ProjectInfoGalva info on info.id=part.IdProject
inner Join Customer cust on cust.ID=info.FK_Customer

Where Convert(Date,[GalvaQualityDailyFicheControle].[CreationDate]) = @Date AND [GalvaQualityDailyFicheControle].FK_idNextProcess=13
Group By
   Concat(Year(info.[CreationDate]),'/',Trim('BS-' from info.ProjectN)) 
  ,part.Designation 
  ,cust.Name   
  ,prod.[Quantity] 
  ,[GalvaQualityDailyFicheControle].[CreationDate] 
)
 Select STRING_AGG(AllProjectN, ' ;') AllProjectN
  ,STRING_AGG(AllDesignation, ' ;')AllDesignation
  ,STRING_AGG(AllCustomer, ' ;')AllCustomer
  ,STRING_AGG(AllQuantity, ' ;')AllQuantity
  ,STRING_AGG(Echantillonnage, ' ;')Echantillonnage
 ,CreationDate
FROM myCTE
Group By CreationDate
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...