SQL - 仅返回负和的值
Posted
技术标签:
【中文标题】SQL - 仅返回负和的值【英文标题】:SQL - return only values where its a negative sum 【发布时间】:2021-07-29 17:46:16 【问题描述】:下午好 - 抱歉这个愚蠢的问题,我似乎无法弄清楚如何引入 WHERE 语句来进行计算以仅返回负 REV_DIFF 值((SUM(PREVIOUS_REVENUE) - SUM(CURRENT_REVENUE)) REV_DIFF)。 请原谅格式 - 仍在学习... 谢谢!
WITH X AS
(
SELECT
1 TYPE, I.NAME COMPANY, C.CLIENT_ID, C.NAME CLIENT_NAME, T.BILL_NUMBER CURRENT_FB, T.WEIGHT CURRENT_WEIGHT,
(T.CHARGES + T.XCHARGES) * CASE WHEN T.TOTAL_CHARGES = 0 THEN 0 ELSE (T.FUNCTIONAL_AMT / T.TOTAL_CHARGES) END CURRENT_REVENUE,
NULL PREVIOUS_FB, NULL PREVIOUS_WEIGHT, NULL PREVIOUS_REVENUE
FROM TLORDER T
INNER JOIN CLIENT C ON C.CLIENT_ID = T.BILL_TO_CODE
INNER JOIN COMPANY_INFO_SRC I ON I.COMPANY_INFO_ID = T.COMPANY_ID
WHERE 1 = 1
AND COALESCE(T.INTERFACE_STATUS_F,0) <> -1
AND T.EXTRA_STOPS <> 'Child'
AND T.BILL_NUMBER NOT IN ('0', 'NA')
AND I.COMPANY_INFO_ID = 3
AND DATE(T.PICK_UP_BY) BETWEEN CURRENT TIMESTAMP - 90 days AND CURRENT TIMESTAMP -60 days
UNION ALL
SELECT
2 TYPE, I.NAME COMPANY, C.CLIENT_ID, C.NAME CLIENT_NAME, NULL CURRENT_FB, NULL CURRENT_WEIGHT, NULL CURRENT_REVENUE, T.BILL_NUMBER PREVIOUS_FB, T.WEIGHT PREVIOUS_WEIGHT,
(T.CHARGES + T.XCHARGES) * CASE WHEN T.TOTAL_CHARGES = 0 THEN 0 ELSE (T.FUNCTIONAL_AMT / T.TOTAL_CHARGES) END PREVIOUS_REVENUE
FROM TLORDER T
INNER JOIN CLIENT C ON C.CLIENT_ID = T.BILL_TO_CODE
INNER JOIN COMPANY_INFO_SRC I ON I.COMPANY_INFO_ID = T.COMPANY_ID
WHERE 1 = 1
AND COALESCE(T.INTERFACE_STATUS_F,0) <> -1
AND T.EXTRA_STOPS <> 'Child'
AND T.BILL_NUMBER NOT IN ('0', 'NA')
AND I.COMPANY_INFO_ID = 3
AND DATE(T.PICK_UP_BY) BETWEEN CURRENT TIMESTAMP - 60 days AND CURRENT TIMESTAMP
)
SELECT
1 SORT, COMPANY, CLIENT_ID, CLIENT_NAME,
COUNT(DISTINCT CURRENT_FB) CURRENT_FB, COUNT(DISTINCT PREVIOUS_FB) PREVIOUS_FB,
ROUND(CASE WHEN COUNT(PREVIOUS_FB) <> 0 THEN (COUNT(CURRENT_FB) - COUNT(PREVIOUS_FB)) / CAST(COUNT(PREVIOUS_FB) AS DOUBLE) * 100.00 ELSE 0 END, 2) FB_DIFF,
SUM(CURRENT_WEIGHT) CURRENT_WEIGHT, SUM(PREVIOUS_WEIGHT) PREVIOUS_WEIGHT,
ROUND(CASE WHEN SUM(PREVIOUS_WEIGHT) <> 0 THEN (SUM(CURRENT_WEIGHT) - SUM(PREVIOUS_WEIGHT)) / SUM(PREVIOUS_WEIGHT) * 100.00 ELSE 0 END, 2) WEIGHT_DIFF,
SUM(CURRENT_REVENUE) CURRENT_REVENUE, SUM(PREVIOUS_REVENUE) PREVIOUS_REVENUE,
ROUND(CASE WHEN SUM(PREVIOUS_REVENUE) <> 0 THEN (SUM(CURRENT_REVENUE) - SUM(PREVIOUS_REVENUE)) / SUM(PREVIOUS_REVENUE) * 100.00 ELSE 0 END, 2) REVENUE_DIFF,
(SUM(PREVIOUS_REVENUE) - SUM(CURRENT_REVENUE)) REV_DIFF
FROM X
GROUP BY COMPANY, CLIENT_ID, CLIENT_NAME
UNION ALL
SELECT
2 SORT, COMPANY, 'TOTAL' CLIENT_ID, NULL CLIENT_NAME,
COUNT(DISTINCT CURRENT_FB) CURRENT_FB, COUNT(DISTINCT PREVIOUS_FB) PREVIOUS_FB,
ROUND(CASE WHEN COUNT(PREVIOUS_FB) <> 0 THEN (COUNT(CURRENT_FB) - COUNT(PREVIOUS_FB)) / CAST(COUNT(PREVIOUS_FB) AS DOUBLE) * 100.00 ELSE 0 END, 2) FB_DIFF,
SUM(CURRENT_WEIGHT) CURRENT_WEIGHT, SUM(PREVIOUS_WEIGHT) PREVIOUS_WEIGHT,
ROUND(CASE WHEN SUM(PREVIOUS_WEIGHT) <> 0 THEN (SUM(CURRENT_WEIGHT) - SUM(PREVIOUS_WEIGHT)) / SUM(PREVIOUS_WEIGHT) * 100.00 ELSE 0 END, 2) WEIGHT_DIFF,
SUM(CURRENT_REVENUE) CURRENT_REVENUE, SUM(PREVIOUS_REVENUE) PREVIOUS_REVENUE,
ROUND(CASE WHEN SUM(PREVIOUS_REVENUE) <> 0 THEN (SUM(CURRENT_REVENUE) - SUM(PREVIOUS_REVENUE)) / SUM(PREVIOUS_REVENUE) * 100.00 ELSE 0 END, 2) REVENUE_DIFF,
(SUM(PREVIOUS_REVENUE) - SUM(CURRENT_REVENUE)) REV_DIFF
FROM X
GROUP BY COMPANY
ORDER BY REV_DIFF
【问题讨论】:
【参考方案1】:您可以使用HAING
子句和GROUP BY
来过滤具有负值的记录。在GROUP BY
之后使用这个。
HAVING (SUM(PREVIOUS_REVENUE) - SUM(CURRENT_REVENUE))) < 0
【讨论】:
太棒了!起初我无法让它工作,然后我意识到我需要将此条件添加到上下查询中。非常感谢!以上是关于SQL - 仅返回负和的值的主要内容,如果未能解决你的问题,请参考以下文章