Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6970419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:42:28+00:00 2026-05-27T16:42:28+00:00

I am having a table ItemTran in sql 2005 with the following columns RowNo

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:42:29+00:00Added an answer on May 27, 2026 at 4:42 pm

    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:

    SELECT x.RowNo, x.Period, x.ItemID
      FROM ( SELECT RowNo, Period, ItemID
                   ,SUM(RcptQty) OVER(PARTITION BY ItemId ORDER BY ItemId, period) AS running_total
               FROM ItemTran t
           ) x
          ,ItemQty q
      WHERE q.Qty = x.running_total
        AND q.ItemID = x.ItemID
    

    This statement returns the last month before the threshold was hit:

     SELECT x.ItemID, MAX(x.Period)
      FROM ( SELECT RowNo, Period, ItemID
                   ,SUM(RcptQty) OVER(PARTITION BY ItemId ORDER BY ItemId, period) AS running_total
               FROM ItemTran t
           ) x
          ,ItemQty q
      WHERE q.Qty <= x.running_total
        AND q.ItemID = x.ItemID
      GROUP BY x.ItemID
    

    If someone else wants to play with this, here are the creation scripts I used:

    CREATE TABLE ItemTran(RowNo int, ItemID varchar(16), Period date, RcptQty int);
    
    INSERT INTO ItemTran VALUES(1, 'ITEM-A', '2011-01-01', 15);
    INSERT INTO ItemTran VALUES(2, 'ITEM-A', '2011-02-01', 10);
    INSERT INTO ItemTran VALUES(3, 'ITEM-A', '2011-03-01', 18);
    INSERT INTO ItemTran VALUES(4, 'ITEM-A', '2011-04-01', 25);
    INSERT INTO ItemTran VALUES(5, 'ITEM-A', '2011-05-01', 3);
    INSERT INTO ItemTran VALUES(6, 'ITEM-A', '2011-06-01', 12);
    INSERT INTO ItemTran VALUES(7, 'ITEM-A', '2011-07-01', 15);
    INSERT INTO ItemTran VALUES(8, 'ITEM-A', '2011-08-01', 5);
    INSERT INTO ItemTran VALUES(9, 'ITEM-A', '2011-09-01', 15);
    INSERT INTO ItemTran VALUES(10, 'ITEM-A', '2011-10-01', 8);
    INSERT INTO ItemTran VALUES(11, 'ITEM-A', '2011-11-01', 10);
    INSERT INTO ItemTran VALUES(12, 'ITEM-A', '2011-12-01', 5);
    
    CREATE TABLE ItemQty(ItemID varchar(16), Qty int);
    
    INSERT INTO ItemQty VALUES('ITEM-A', 68);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a table with 60 columns, and 200 rows. Altering a BIT column from
I am having a table Table1 with columns id1, id2, id3 all the columns
I have a table having 2 columns EmployeeId (int) and EmployeeDetails(XMl type) EmployeeId EmployeeDetails
iam having a table with columns like id || counter if i do something
I have MSSQL table having columns, serves as storage for items for user with
i am having a table with 8 column and following values 1 2 3
Is there a way to get for a particular table in an SQL DataBase,
I am having a table that has 2500 rows. There is a column Income
Suppose i am having a table employee with 2 columns emp_ID and e_name and
I have written PLSQL program that generates table having usually more than 200 columns.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.