开发者社区> 问答> 正文

使用STRING_AGG选择不同的值

我想选择不同的客户,所以我写了这个查询:

SELECT 
   STRING_AGG( ISNULL(Concat(Year(info.[CreationDate]),'/',Trim('BS-' from info.ProjectN)) , ' '), ' ;')  As 'AllProjectN'
  ,STRING_AGG( ISNULL(part.Designation , ' '), ' ;')  As 'AllDesignation' 
  ,STRING_AGG( ISNULL([GalvaQualityDailyFicheControle].[Quantity] , ' '), ' ;')  As 'AllQuantity'
  ,[GalvaQualityDailyFicheControle].[CreationDate]

  ,(SELECT STRING_AGG( ISNULL(cust.Name , ' '), ' ;')
   WITHIN GROUP (ORDER BY cust.Name)
   FROM (SELECT distinct cust.Name FROM Customer)as x) AS AllCustomer

  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
  Group By cust.Name,[GalvaQualityDailyFicheControle].[CreationDate]

但是当我运行它时,我得到了重复的客户

展开
收起
祖安文状元 2020-01-04 15:21:05 1148 0
1 条回答
写回答
取消 提交回答
  • 答案1:

    SELECT 
        STRING_AGG( ISNULL(Concat(Year(info.[CreationDate]),'/',Trim('BS-' from info.ProjectN)) , ' '), ' ;')  As 'AllProjectN'
        ,STRING_AGG( ISNULL(part.Designation , ' '), ' ;')  As 'AllDesignation' 
        ,STRING_AGG( ISNULL([GalvaQualityDailyFicheControle].[Quantity] , ' '), ' ;')  As 'AllQuantity'
        ,[GalvaQualityDailyFicheControle].[CreationDate]
        ,STRING_AGG( ISNULL(cust.Name , ' '), ' ;') WITHIN GROUP (ORDER BY cust.Name) AS AllCustomer
    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
    Group By [GalvaQualityDailyFicheControle].[CreationDate]
    
    2020-01-04 15:21:27
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载