通过其他列

时间:2018-06-01 13:01:20

标签: sql sql-server-2014

我正在尝试编写一个简单的查询,以便为每个 INV_CART_ID 获取MAX DEMAND_DATE 。这是我现有的查询:

SELECT BUSINESS_UNIT, INV_CART_ID, INV_ITEM_ID, CART_COUNT_QTY, DEMAND_DATE
FROM PS_CART_CT_INF_INV A
WHERE A.INV_ITEM_ID = 1
AND A.BUSINESS_UNIT = '11MMS'
AND A.CART_COUNT_QTY <> 0
ORDER BY DEMAND_DATE DESC

当前输出:

enter image description here

期望输出:

BUSINESS_UNIT  INV_CART_ID     INV_ITEM_ID     CART_COUNT_QTY    DEMAND_DATE
11MMS          405              1               5.0000           2018-05-29
11MMS          OUTPT_INFUSION   1               4.0000           2018-05-29
11MMS          938              1               15.0000          2018-05-31
11MMS          286              1               1.0000           2018-05-07
11MMS          708              1               4.0000           2018-04-05

这是我到目前为止所做的尝试:

SELECT MAX(DEMAND_DATE) AS DEMAND_DATE, INV_CART_ID, BUSINESS_UNIT, 
 INV_ITEM_ID, CART_COUNT_QTY
FROM PS_CART_CT_INF_INV A
WHERE A.INV_ITEM_ID = 1
 AND A.BUSINESS_UNIT = '11MMS'
 AND A.CART_COUNT_QTY <> 0
 AND A.DEMAND_DATE IN (SELECT MAX (DEMAND_DATE) FROM PS_CART_CT_INF_INV B 
   WHERE A.INV_ITEM_ID = B.INV_ITEM_ID GROUP BY INV_CART_ID)
GROUP BY INV_CART_ID, BUSINESS_UNIT, INV_ITEM_ID, CART_COUNT_QTY

然而,它并未返回所有 INV_CART_ID #&#39;并且未检索到正确的行(错误 DEMAND_DATE ):

enter image description here

2 个答案:

答案 0 :(得分:1)

使用 if UIDevice.current.isJailBroken() { // show a blank screen or some other view controller let jailbreakVC = JailBrokenViewController() self.navigationController?.present(jailbreakVC, animated: true, completion:nil) } else { // continue executing your next View controller let nextVC = NextViewController() self.navigationController?.present(nextVC, animated: true, completion:nil) }

ROW_NUMBER

如果您不想使用分析功能,那么我仍然不会使用您当前的方法。相反,我会加入一个子查询,如下所示:

WITH cte AS (
    SELECT BUSINESS_UNIT, INV_CART_ID, INV_ITEM_ID, CART_COUNT_QTY, DEMAND_DATE,
        ROW_NUMBER() OVER (PARTITION BY INV_CART_ID ORDER BY DEMAND_DATE DESC) rn
    FROM PS_CART_CT_INF_INV
    WHERE
        INV_ITEM_ID = 1 AND
        BUSINESS_UNIT = '11MMS' AND
        CART_COUNT_QTY <> 0
)

SELECT
    BUSINESS_UNIT, INV_CART_ID, INV_ITEM_ID, CART_COUNT_QTY, DEMAND_DATE
FROM cte
WHERE rn = 1
ORDER BY DEMAND_DATE DESC;

您当前查询的问题,即使经过纠正,也是在SELECT t1.BUSINESS_UNIT, t1.INV_CART_ID, t1.INV_ITEM_ID, t1.CART_COUNT_QTY, t1.DEMAND_DATE FROM PS_CART_CT_INF_INV t1 INNER JOIN ( SELECT INV_CART_ID, MAX(DEMAND_DATE) AS MAX_DEMAND_DATE FROM PS_CART_CT_INF_INV WHERE INV_ITEM_ID = 1 AND BUSINESS_UNIT = '11MMS' AND CART_COUNT_QTY <> 0 GROUP BY INV_CART_ID ) t2 ON t1.INV_CART_ID = t2.INV_CART_ID AND t1.DEMAND_DATE = t2.MAX_DEMAND_DATE WHERE t1.INV_ITEM_ID = 1 AND t1.BUSINESS_UNIT = '11MMS' AND t1.CART_COUNT_QTY <> 0; 子句中使用相关子查询。众所周知,这些是潜在的性能杀手,如果可能的话应该避免使用。

答案 1 :(得分:0)

我想你想要:

ぼ abc 12

但是,这会为您提供重复记录,如果您想避免重复,那么您可以在SELECT BUSINESS_UNIT, INV_CART_ID, INV_ITEM_ID, CART_COUNT_QTY, DEMAND_DATE FROM PS_CART_CT_INF_INV AS a WHERE INV_ITEM_ID = 1 AND BUSINESS_UNIT = '11MMS' AND CART_COUNT_QTY <> 0 AND DEMAND_DATE = (SELECT MAX(b.DEMAND_DATE) FROM PS_CART_CT_INF_INV as b WHERE a.INV_CART_ID = b.INV_CART_ID ); 子句中使用identity列或pk

WHERE