在 s-s-rS 中使用 STUFF 之前如何排序

Posted

技术标签:

【中文标题】在 s-s-rS 中使用 STUFF 之前如何排序【英文标题】:How to sort before using STUFF in s-s-rS 【发布时间】:2017-08-23 22:07:30 【问题描述】:

我在这里尝试填充 2 个不同的值。这是数量+价格。例如:1-0.36 美元; 100-0.29 美元; 25-0.31 美元。在填充之前如何按数量排序? (1,25,100 而不是 1,100,25)我确实遇到了这个链接 Sort data before concatenating using STUFF FOR XML,但它处理 1 个值,而我正在处理 2 个值

    SELECT STUFF(
   (SELECT DISTINCT TOP (5) 
        '; ' + (CAST(FLOOR(CASE WHEN PCFBD.Quantity IS NOT NULL THEN PCFBD.Quantity ELSE 1 END) AS VARCHAR)  + '-$' + CAST(REPLACE(REPLACE(RTRIM(REPLACE(
       CASE WHEN PCF.PriceMethod = 0 THEN ROUND(I.CdCost / (100 - PCF.FormulaPercent) * 100, 2) 
            WHEN PCFBH.PriceFormula = 2 AND PCFBD.FormulaPercent IS NULL THEN ROUND(I.CdCost / (100 - PCF.FormulaPercent) * 100, 2)
            WHEN PCFBH.PriceFormula = 2 AND PCFBD.FormulaPercent IS NOT NULL THEN ROUND(I.CdCost / (100 - PCFBD.FormulaPercent) * 100, 2)
            WHEN PCFBH.PriceFormula = 1 THEN ROUND((I.ListPrice * (100 - PCFBD.FormulaPercent)) * .01,2)
            ELSE NULL END, '000' ,'')), ' ','0') + '', '.  ', '') AS VARCHAR))
 FROM              Item AS I 
 INNER JOIN        PriceContractFamily AS PCF ON I.FamilyId = PCF.FamilyId
                 AND I.ItemStatus IN (0, 5) 
 INNER JOIN        StockItem SI ON I.ItemId = SI.ItemId
                 AND SI.WarehouseId = '502E5876-C26B-4E11-8B88-AFE0C34ECF0D'                     
 LEFT OUTER JOIN   PriceContractFamilyBracketHeader AS PCFBH ON PCF.PriceContractFamilyId = PCFBH.PriceContractFamilyId 
 LEFT OUTER JOIN   PriceContractFamilyBracketDetail AS PCFBD ON PCFBH.BracketHeaderId = PCFBD.BracketHeaderId
 WHERE I.ListPrice = @ListPrice
AND LEFT(I.ItemNumber, 6) = @ItemNumber
AND PCF.PriceContractId = @PriceContractId  
 FOR XML PATH('')),1, 2, '') AS QtyPrice                                                        

【问题讨论】:

【参考方案1】:

您应该能够在 FOR XML PATH 语句之前添加 ORDER BY。

【讨论】:

以上是关于在 s-s-rS 中使用 STUFF 之前如何排序的主要内容,如果未能解决你的问题,请参考以下文章

如何在 s-s-rS 2008 中按聚合/计算列交互排序

有没有办法在 STUFF/FOR XML PATH 中对数值进行排序?

如何在带有参数的 s-s-rS 查询设计器中使用 IF ELSE 语句?

带有手动添加列的 s-s-rS 排序矩阵

s-s-rS 2008 排序不工作

订购 s-s-rS 报告栏