sql server - SQL sum columns depend on the value of other column -
sql server - SQL sum columns depend on the value of other column -
i've got table fields:
doc_no , doc_type , item_no , added_qty ,deducted_qty
i need query returning result set based on
if doc_type= "2" select sum(added_qty) sum_added_qty
if doc_type= "7" select sum(deducted_qty) sum_deducted_qty
and show sum_added_qty - sum_deducted_qty stock
the result must on same row
-------------------------------------------------------- | item_no | sum_added_qty | sum_deducted_qty | stock| --------------------------------------------------------
thanks help!
(system sql server)
select item_no, sum(case when doc_type = '2' added_qty else 0 end) sum_added_qty, sum(case when doc_type = '7' deducted_qty else 0 end) sum_deducted_qty, sum(case when doc_type = '2' added_qty when doc_type = '7' -deducted_qty else 0 end) stock yourtable grouping item_no
sql-server lookup
Comments
Post a Comment