• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

SQL Server 2016 – 东西/ XML路径

sql 来源:RachelShaw 3次浏览

我有这个基本查询,带回配置信息。每个MatrixCellID是一张单独的门票,每个门票可以有多个StatisticalGroupCodesStatisicalGroupDescriptions。目前每个MatrixCellID多次出现,因为其多个StatisicalGroupCodes。我希望“代码”和“描述”列以逗号分隔的一列显示。SQL Server 2016 – 东西/ XML路径

这是我的查询:

select 
    cmc.MatrixSheetId, 
    CMS.MatrixSheetName, 
    cmc.MatrixCellId, 
    CMC.Price, 
    CMC.PriceListId, 
    CPL.PriceListName, 
    CPT.Description as PriceTable, 
    case when CMC.Code <> '' then cmc.code else 'EMPTY' end AS TicketCode, 
    case when CMC.Name <> '' then cmc.name else 'EMPTY' END AS TicketName, 
    case when CMC.Description <> '' then cmc.description else 'EMPTY' END AS TicketDescription, 
    case when CMC.Description2 <> '' then cmc.description2 else 'EMPTY' end AS AdditionalTicketDescription, 
    CASE WHEN CMCI.AccountMandatory = 1 THEN 'YES' else 'NO' end AS AccountMandatory, 
    CASE WHEN CDC.Description IS NULL THEN 'NONE' ELSE CDC.Description END AS AccountCategory, 
    CDT.DocTemplateName AS PrintTemplate, 
    CTP.Description as TaxPackage, 
    CT.TaxName, 
    CASE when CMC.PriceType = 0 then 'Fixed' else 'Variable' end as PriceType, 
    CCC.CostCenterDescription, 
    CCC.CostCenterCode, 
    CCC.CostCenterAK, 
    CSG.StatisticalGroupCode, 
    CSG.StatisticalGroupDescription 
from 
CNF_MatrixCell CMC 
inner join CNF_MatrixSheet CMS on CMC.MatrixSheetId = CMS.MatrixSheetId 
inner join CNF_PriceList CPL on CMC.PriceListId = CPL.PriceListId 
INNER JOIN CNF_MatrixCellInfo CMCI on CMC.MatrixCellId = CMCI.MatrixCellId 
left join CNF_DmgCategory CDC on CMCI.AccountDmgCatId = CDC.DmgCategoryId 
left join CNF_DocTemplate CDT on CMC.DocTemplateId = CDT.DocTemplateId 
LEFT join CNF_TaxPackage CTP on CMC.TaxPackageId = CTP.TaxPackageId 
LEFT join CNF_Tax2Package CT2P on CTP.TaxPackageId = CT2P.TaxPackageId 
LEFT JOIN CNF_Tax CT on CT2P.TaxId = CT.TaxId 
LEFT JOIN CNF_CostCenter_Validity CCCV on CMC.MatrixCellId = cccv.MatrixCellId 
LEFT JOIN CNF_CostCenter CCC on CCCV.CostCenterId = CCC.CostCenterId 
inner join CNF_PriceTable CPT on CMC.PriceTableId = CPT.PriceTableId 
LEFT JOIN CNF_StatisticalGroupValidity CSGV on CMC.MatrixCellId = CSGV.MatrixCellId 
LEFT JOIN CNF_StatisticalGroup CSG on CSGV.StatisticalGroupId = CSG.StatisticalGroupId 
WHERE CMC.Enabled = 1 
    AND CCC.Enabled = 1 
    AND CPT.Enabled = 1 
    AND CMS.Enabled = 1 
ORDER BY 
    CMS.MatrixSheetId, 
    CMC.MatrixCellId, 
    CMC.Code 

我曾尝试使用的东西和XML路径,但不能让它正常工作与我的联接。 预先感谢您。


===========解决方案如下:

您可以使用它。

select 
    cmc.MatrixSheetId, 
    CMS.MatrixSheetName, 
    cmc.MatrixCellId, 
    CMC.Price, 
    CMC.PriceListId, 
    CPL.PriceListName, 
    CPT.Description as PriceTable, 
    case when CMC.Code <> '' then cmc.code else 'EMPTY' end AS TicketCode, 
    case when CMC.Name <> '' then cmc.name else 'EMPTY' END AS TicketName, 
    case when CMC.Description <> '' then cmc.description else 'EMPTY' END AS TicketDescription, 
    case when CMC.Description2 <> '' then cmc.description2 else 'EMPTY' end AS AdditionalTicketDescription, 
    CASE WHEN CMCI.AccountMandatory = 1 THEN 'YES' else 'NO' end AS AccountMandatory, 
    CASE WHEN CDC.Description IS NULL THEN 'NONE' ELSE CDC.Description END AS AccountCategory, 
    CDT.DocTemplateName AS PrintTemplate, 
    CTP.Description as TaxPackage, 
    CT.TaxName, 
    CASE when CMC.PriceType = 0 then 'Fixed' else 'Variable' end as PriceType, 
    CCC.CostCenterDescription, 
    CCC.CostCenterCode, 
    CCC.CostCenterAK, 
    STUFF(StatGrpCode.StatGroupCodes,1,1,'') StatGroupCodes, 
    STUFF(StatGrpCodeDesc.StatGroupDescription,1,1,'') StatGroupDescription 
from 
    CNF_MatrixCell CMC 
    inner join CNF_MatrixSheet CMS on CMC.MatrixSheetId = CMS.MatrixSheetId 
    inner join CNF_PriceList CPL on CMC.PriceListId = CPL.PriceListId 
    INNER JOIN CNF_MatrixCellInfo CMCI on CMC.MatrixCellId = CMCI.MatrixCellId 
    left join CNF_DmgCategory CDC on CMCI.AccountDmgCatId = CDC.DmgCategoryId 
    left join CNF_DocTemplate CDT on CMC.DocTemplateId = CDT.DocTemplateId 
    LEFT join CNF_TaxPackage CTP on CMC.TaxPackageId = CTP.TaxPackageId 
    LEFT join CNF_Tax2Package CT2P on CTP.TaxPackageId = CT2P.TaxPackageId 
    LEFT JOIN CNF_Tax CT on CT2P.TaxId = CT.TaxId 
    LEFT JOIN CNF_CostCenter_Validity CCCV on CMC.MatrixCellId = cccv.MatrixCellId 
    LEFT JOIN CNF_CostCenter CCC on CCCV.CostCenterId = CCC.CostCenterId 
    inner join CNF_PriceTable CPT on CMC.PriceTableId = CPT.PriceTableId 
    LEFT JOIN CNF_StatisticalGroupValidity CSGV on CMC.MatrixCellId = CSGV.MatrixCellId 
    OUTER APPLY 
     (SELECT ', ' + CSG.StatisticalGroupCode 
      FROM CNF_StatisticalGroup CSG 
      WHERE CSGV.StatisticalGroupId = CSG.StatisticalGroupId FOR XML PATH('')) StatGrpCode (StatGroupCodes) 
    OUTER APPLY 
     (SELECT ', ' + CSG.StatisticalGroupDescription 
      FROM CNF_StatisticalGroup CSG 
      WHERE CSGV.StatisticalGroupId = CSG.StatisticalGroupId FOR XML PATH('')) StatGrpCodeDesc (StatGroupDescription) 
WHERE CMC.Enabled = 1 
    AND CCC.Enabled = 1 
    AND CPT.Enabled = 1 
    AND CMS.Enabled = 1 

ORDER BY 
    CMS.MatrixSheetId, 
    CMC.MatrixCellId, 
    CMC.Code 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)