苦苦于条件逻辑

时间:2017-07-26 20:04:15

标签: sql-server tsql ssms

已编辑的问题:

我似乎无法弄清楚我的查询的条件逻辑。

我确信这很简单,但我已经在这个问题上旋转了很长时间 - 这只是其中的一天。

任何帮助总是受到赞赏。

CURRENT QUERY:

SELECT
    r.WidgetPK
    ,r.WidgetName
    ,r.WeightRateFlag                       [WeightRateFlag]
    ,r.Rate                                 [Rate]
    ,r.Breakpoint                           [Breakpoint]
    ,MAX(ISNULL(f.ShippingFee,0))           [ShippingFee]
    ,MAX(ISNULL(f.OtherFee,0))              [OtherFee]
    ,MAX(r.weight)                          [Weight]
FROM
    #Rates r
    LEFT JOIN #Fees f ON f.WidgetPK = r.WidgetPK

为简单起见,我省略了GROUP BY。

如果WeightRateFlag在每个WidgetPK的任意行中都有一个1,则不会返回所有带0的行。如果WeightRateFlag没有包含1的行,则返回所有行。

抱歉原来的问题不明确 - 搜索没有帮助,我问了一位同事。我认为我的问题可能只是我在这里和我的搜索中提出了错误的问题。

2 个答案:

答案 0 :(得分:1)

SELECT
r.WidgetPK
,r.WidgetName
,r.WeightRateFlag                       [WeightRateFlag]
,r.Rate                                 [Rate]
,r.Breakpoint                           [Breakpoint]
,MAX(ISNULL(f.ShippingFee,0))           [ShippingFee]
,MAX(ISNULL(f.OtherFee,0))              [OtherFee]
,MAX(r.weight)                          [Weight]
FROM
#Rates r
LEFT JOIN #Fees f ON f.WidgetPK = r.WidgetPK
WHERE r.WeightRateFlag = 1
UNION ALL 
SELECT
r.WidgetPK
,r.WidgetName
,r.WeightRateFlag                       [WeightRateFlag]
,r.Rate                                 [Rate]
,r.Breakpoint                           [Breakpoint]
,MAX(ISNULL(f.ShippingFee,0))           [ShippingFee]
,MAX(ISNULL(f.OtherFee,0))              [OtherFee]
,MAX(r.weight)                          [Weight]
FROM
#Rates r
LEFT JOIN #Fees f ON f.WidgetPK = r.WidgetPK
WHERE r.WeightRateFlag = 0 
AND NOT EXISTS (SELECT * FROM #rates r2  WHERE r2WeightRateFlag =1 AND r.WidgetName = r2.WidgetName)

答案 1 :(得分:0)

有点复杂,但CTE非常有帮助。此外,他们应该与优化器一起工作得很好。修改您需要的列。

/* TEST DATA SETUP */
IF OBJECT_ID(N'tempdb..#t1') IS NOT NULL
BEGIN
     DROP TABLE #t1
END
CREATE TABLE #t1 (WidgetPK int, col1 varchar(19), WeightRateFlag bit, ShippingFee money, OtherFee money, [Weight] int);
INSERT INTO #t1 (WidgetPK, col1, WeightRateFlag, ShippingFee, OtherFee, [Weight])
VALUES
      (1, 'showme1', 1, 9, 1, 1)
    , (1, 'noshow2', 0, 2, 9, 2)
    , (1, 'noshow3', 0, 1, 2, 9)
    , (2, 'showme1', 1, 9, 9, 9)
    , (3, 'showme1', 0, 1, 9, 1)
    , (3, 'showme2', 0, 9, 9, 9)
    , (3, 'showme3', 0, 9, 1, 9)
;

/* QUERY STARTS HERE */
WITH cte1 AS (
    SELECT x1.* 
    FROM (
        SELECT #t1.WidgetPK, #t1.col1, #t1.WeightRateFlag, #t1.ShippingFee, #t1.OtherFee, #t1.[Weight]
            , RANK() OVER (PARTITION BY #t1.WidgetPK ORDER BY #t1.WeightRateFlag DESC) AS rn
        FROM #t1
    ) x1 WHERE x1.rn = 1

)
, cte2 AS (
    SELECT cte1.WidgetPK
        , MAX(cte1.ShippingFee) AS ShippingFee
        , MAX(cte1.OtherFee) AS OtherFee
        , MAX(cte1.[Weight]) AS [Weight]
    FROM cte1
    GROUP BY cte1.WidgetPK
)
SELECT cte1.WidgetPK, cte1.col1, cte1.WeightRateFlag, cte2.ShippingFee, cte2.OtherFee, cte2.[Weight]
FROM cte1
LEFT OUTER JOIN cte2 ON cte1.WidgetPK = cte2.WidgetPK
;