以下查询排除了所有产品,但我尝试仅在R.OPERATING_UNITS = 'WP'
时排除产品。我不确定是否需要在case子句中包含case语句。我想知道最有效的方法是什么。下面是查询,RESOURCE
表以及所需的结果集。为了便于解释,我简化了表和查询。
SELECT R.DEPTID,
R.FISCAL_YEAR,
sum(R.AMOUNT) total
FROM RESOURCE R
WHERE
R.PRODUCT_ID NOT IN (
SELECT PRODUCT_ID FROM PRODUCT WHERE PRODUCT_CAT='FUEL' )
group by R.FISCAL_YEAR,R.DEPTID
RESOURCE表
DPTID FISCAL_YEAR OPERATING_UNIT AMOUNT
PTT 2017 WP 1200
PTT 2017 SP 3000
PTT 2017 GP 1000
FPP 2017 WP 1000
FPP 2018 GP 2000
FPP 2017 SP 1000
FPP 2018 SP 2000
结果集。请注意,在计算总和时忽略WP。
2017 PTT 4000
2017 FPP 1000
2018 FPP 4000
答案 0 :(得分:0)
FISCAL_YEAR DPTID Amount
2017 PTT 4000
2017 FPP 1000
2018 FPP 4000
输出:
text = "vagrant 11450 4344 0 Feb22 pts/2 00:00:28 python run.py abc"
答案 1 :(得分:0)
我相信你只需要通过这样的所有非WP产品:
SELECT R.DEPTID, R.FISCAL_YEAR, SUM(R.AMOUNT) total FROM RESOURCE R
WHERE
R.PRODUCT_ID NOT IN (SELECT PRODUCT_ID FROM PRODUCT WHERE PRODUCT_CAT = 'FUEL')
OR R.OPERATING_UNITS <> 'WP' -- <<<========
GROUP BY R.DEPTID, R.FISCAL_YEAR
当然你也可以通过连接来处理这个问题,因为我认为内连接会起作用并且是一对一的关系:
SELECT R.DEPTID, R.FISCAL_YEAR, SUM(R.AMOUNT) total
FROM RESOURCE R INNER JOIN PRODUCT P ON P.PRODUCT_ID = R.PRODUCT_ID
WHERE
P.PRODUCT_CAT <> 'FUEL' OR R.OPERATING_UNITS <> 'WP'
-- by DeMorgan's Law this is equivalent:
-- NOT (P.PRODUCT_CAT = 'FUEL' AND R.OPERATING_UNITS = 'WP')
GROUP BY R.DEPTID, R.FISCAL_YEAR