[英]find the user entered value or sum the row values
我需要獲取在價格列中具有特定值的發票號,如果不是所有在價格列中具有該特定值總和值的發票號..如何為此編寫 sql 查詢
我附上了我的表數據的數據
invoice price v1 1000 v2 200 v3 35 v4 500 v5 50 v6 300 v7 400
預期輸出:
if(input 500) found =>then found v4
其他((v2,v6)的總和
您可以找到符合條件的 1 或 2 張發票的所有組合:
select d1.invoice, d1.price,
(case when d2.invoice <> d1.invoice then d2.invoice end) as invoice2,
(case when d2.invoice <> d1.invoice then d2.price end) as price2
from data d1 left join
data d2
on d1.invoice <= d2.invoice
where (d1.price = 500 and d2.invoice = d1.invoice) or
(d1.price + d2.price) = 500
這是一個 db<>fiddle。
如果您想要更大的組合,請添加更多連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.