[英]Add new column based on conditions and the value difference of other column
我想我可能在這里忽略了一些明顯的東西。 我不是要求某人為我編寫整個代碼,我只需要提示或類似案例的鏈接。
我的查詢:
select Client , ProductID, M_POS_TYPE AS Keep_or_Keep_in_Transit, Amount
FROM inventory_table inv_table
JOIN inventory_position inv_pos
ON inv_pos.ProductID=inv_table.ProductID
group by Client, ProductID, M_POS_TYPE, Amount
輸出:
如何添加一個新列來檢查是否減去列中的值: Amount
is different from 0 for same ProductID
and Client
?
期望:
5-4<>0
然后
(Y)
我嘗試使用的是條件語句CASE
語句,但我如何確保它會計算相同Client
和ProductID
的差異?
我正在尋找一般情況下的解決方案,表中有數千個不同的 ProductsID 和 Clients 值。 我有點陷入這個問題。
因為您只要求提示或提示。 沒有示例或數據測試,僅作為簡短說明。 谷歌搜索“分析函數”可以幫助您解決它。 因此,您可以總結每個客戶的 ProductID,然后在 CASE 中進行比較。
也許是這樣的。
select Client , ProductID, M_POS_TYPE AS Keep_or_Keep_in_Transit, Amount,
sum(case when M_POS_TYPE = 'Keep_Transit' then AMOUNT*-1 else AMOUNT end) over (partition by Client, ProductID) as DIFF
FROM inventory_table inv_table
JOIN inventory_position inv_pos
ON inv_pos.ProductID=inv_table.ProductID
group by Client, ProductID, M_POS_TYPE, Amount
希望你能先行一步。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.