【问题标题】:Return values for X and Y where X-Y = Max(X-Y)返回 X 和 Y 的值,其中 X-Y = Max(X-Y)
【发布时间】:2013-04-22 22:53:22
【问题描述】:

(SQL SERVER 2005)

我有一个与 ItemCode 相关的多个产品的表格。我可以使用下面的查询(我认为)建立最佳节省,但我需要包括 RRP 和 SellingPrice 字段,以提供最佳节省的组合。

提前道歉这可能是一个常见问题,但我找不到合适的解决方案。

SELECT     ItemCode, MAX(RRP - [SellingPrice]) AS BestSaving
       FROM          ItemCodePricingDetail
       WHERE      ([ProductGroup] = N'SHOES') AND ([Stock Flag] = N'Y') 
                 AND (RRP > 0) AND ([SellingPrice] > 0)
       GROUP BY ItemCode

非常感谢

【问题讨论】:

    标签: tsql aggregation


    【解决方案1】:
    select * from ItemCodePricingDetail
    JOIN
    (
    SELECT     ItemCode, MAX(RRP - [SellingPrice]) AS BestSaving
           FROM          ItemCodePricingDetail
           WHERE      ([ProductGroup] = N'SHOES') AND ([Stock Flag] = N'Y') 
                     AND (RRP > 0) AND ([SellingPrice] > 0)
           GROUP BY ItemCode
    ) as t1  on ItemCodePricingDetail.ItemCode=t1.ItemCode 
                and RRP - [SellingPrice]= t1.BestSaving  
    

    【讨论】:

    • 非常好。我不知道你可以加入“RRP - [SellingPrice]= t1.BestSaving”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多