I am having a table ItemTran in sql 2005 with the following columns
RowNo Item ID Period RcptQty
1 ITEM-A 2011-01 15
2 ITEM-A 2011-02 10
3 ITEM-A 2011-03 18
4 ITEM-A 2011-04 25
5 ITEM-A 2011-05 3
6 ITEM-A 2011-06 12
7 ITEM-A 2011-07 15
8 ITEM-A 2011-08 5
9 ITEM-A 2011-09 15
10 ITEM-A 2011-10 8
11 ITEM-A 2011-11 10
12 ITEM-A 2011-12 5
There is a another table ItemQty with columns
Item ID Qty
ITEM-A 78
How can i return the last rows from table ItemTran matching with RcptQty with the Table Column “Qty” from ItemQty Table.
i want the output like this
4 ITEM-A 2011-04 25
5 ITEM-A 2011-05 3
6 ITEM-A 2011-06 12
7 ITEM-A 2011-07 15
8 ITEM-A 2011-08 5
9 ITEM-A 2011-09 15
10 ITEM-A 2011-10 8
11 ITEM-A 2011-11 10
12 ITEM-A 2011-12 5
i want to fetch the last rows from ItemTran where the itemQty.Qty matches.
basically i want to get the last rows from Table A with summary matching with Table B.
Please help me on this. i am very new to sql.
Thanks
I’m going to assume you meant “68” in ItemQty and that you are adding up the quantity for each month until you hit that magic number. Then you are looking for what month that target quantity was hit. I don’t have sql server running, but the sql should be fairly close.
This statement assumes you really mean the totals match:
This statement returns the last month before the threshold was hit:
If someone else wants to play with this, here are the creation scripts I used: