user3459916
user3459916

Reputation: 1

How to get rows having sum equal to given in SQL

**Table A :**                 **Table B:**
ITEMCODE Total QTY        ITEMCODE Qty
A        100              A        25
B        50               A        75
C        75               B        10
D        85               B        20
                          B        30
                          D        80
                          D        5

Need to result view :

A   100  = A 25
           A 75

How to get this result with SQL function with these two tables

Upvotes: 0

Views: 34

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270513

If I understand correctly, you want the rows where the sum for the itemcode in b matches a. One method uses window functions:

select b.*
from (select b.*, sum(qty) over (partition by itemcode) as sumqty
      from b
     ) b join
     a
     on a.itemcode = b.itemcode and a.qty = b.sumqty;

Upvotes: 1

Related Questions