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 822751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:50:36+00:00 2026-05-15T02:50:36+00:00

I want to compute result from this table. I want quantity 1 – quantity2

  • 0

I want to compute result from this table.
I want quantity 1 – quantity2 as another column in the table shown below.
this table has more such records

I am trying to query but not been able to get result.

select  * from v order by is_active desc, transaction_id desc


PK_GUEST_ITEM_ID FK_GUEST_ID          QUANTITY               TRANSACTION_ID       IS_ACTIVE
---------------- -------------------- ---------------------- -------------------- -----------
12963            559                  82000                  795                  1
12988            559                  79000                  794                  0
12987            559                  76000                  793                  0
12986            559                  73000                  792                  0
12985            559                  70000                  791                  0
12984            559                  67000                  790                  0
12983            559                  64000                  789                  0
12982            559                  61000                  788                  0
12981            559                  58000                  787                  0
12980            559                  55000                  786                  0
12979            559                  52000                  785                  0
12978            559                  49000                  784                  0
12977            559                  46000                  783                  0
12976            559                  43000                  782                  0

I want another column that will contain the subtraction of two quantities .

DESIRED RESULT SHOULD BE SOMETHING LIKE THIS

PK_GUEST_ITEM_ID FK_GUEST_ID          QUANTITY     Result          TRANSACTION_ID       IS_ACTIVE
---------------- -------------------- ---------------------- -------------------- -----------
12963            559                  82000       3000           795                  1
12988            559                  79000       3000           794                  0
12987            559                  76000       3000           793                  0
12986            559                  73000       3000           792                  0
12985            559                  70000       3000           791                  0
12984            559                  67000       3000           790                  0
12983            559                  64000       3000           789                  0
12982            559                  61000       3000           788                  0
12981            559                  58000       3000           787                  0
12980            559                  55000       3000           786                  0
12979            559                  52000       3000           785                  0
12978            559                  49000       3000           784                  0
12977            559                  46000       3000           783                  0
12976            559                  43000       NULL           782                  0
  • 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-15T02:50:37+00:00Added an answer on May 15, 2026 at 2:50 am

    to get the next lower transaction id you can use a subquery

    Select max(transactionid) 
    from vinner 
    where vinner.tr.ansactionid <vouter.transactionid
    

    This works fine for me:

    select v1.transactionid as HigherTransactionID
          ,v2.transactionid as LowerTransactionId
      ,v1.quantity as HigherQuan
      ,v2.quantity as LowerQuan
          ,v1.quantity - v2.quantity as Result
    from v  as v1
         left join v as v2 on
    v2.transactionid = 
                (Select MAX(v.transactionid) 
                 from v 
                  where v.transactionid < v1.transactionid)
    

    Tested with following Table:

    quantity    transactionid
    8200            795
    7900            794
    6600            793
    6300            792
    6000            788
    5700            787
    4300            786
    

    With following result:

    HigherTransactionID LowerTransactionId  HigherQuan  LowerQuan   Result
    795                 794                 8200            7900            300
    794                 793                 7900            6600            1300
    793                 792                     6600            6300            300
    792                 788                     6300            6000            300
    788                 787                     6000            5700            300
    787                 786                     5700            4300            1400
    786                 NULL                    4300            NULL            NULL
    

    Hope this is what you expected

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a table Employee like this EmpID, EmpName 1 , hatem
I want to apply operations from the OpenCV computer vision library, in real time,
This is not homework, for I am not a student. This is for my
I'm computing data from database (~130 000 000 rows). Because of big amount of
I am having issues getting the groups from Active Directory via System.DirectoryServices Originally I
I have CObject as main class and CRock, CDesk, CComputer as derivates from CObject.
I have an sql query (MyQSL DB, using .Net's SqlClient) that returns a dataset.
I'm familiar with the Google Maps API and I'm trying to learned the iOS
I'll describe what I'm trying to do, and hopefully, somebody can tell me what
I am college student (computer science) and have just started a C# programming class.

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.