【问题标题】:Concatenate rows with semicolon separator after grouping分组后用分号分隔符连接行
【发布时间】:2020-09-16 01:20:45
【问题描述】:

我想用分号分隔符连接我的结果,所以这是我的查询

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]

我得到了这个结果

我尝试使用 STRING_AGG 得到一行,但数据重复

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] <=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
  [GalvaQualityDailyFicheControle].[CreationDate]

这是我得到的结果

如何获取一行而不重复数据?

【问题讨论】:

  • 请说明所需的结果。 “一行不重复数据”不清楚。
  • 这能回答你的问题吗? Produce DISTINCT values in STRING_AGG
  • @DavidBrowne-Microsoft 当我运行第一个查询时,我得到两行,当我运行第二个查询时,我应该使用分号作为分隔符从第一行和第二行获取数据,但是当我运行第二个查询时每行重复两次(我应该只得到两个值)
  • @MarcGuillot 非常感谢你的帮助,我也从这个帖子stackoverflow.com/a/4473039/9608194得到了很多帮助

标签: sql-server


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 2018-09-15
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 2021-09-05
    相关资源
    最近更新 更多