Results 1 to 10 of 10

Thread: SQL Question: How to do an "Easy" Substraction

Threaded View

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    SQL Question: How to do an "Easy" Substraction

    Obviously is not as "easy" for me because I can't find a way to do it. But I hope it is for you.

    Here is my problem:

    I have 2 fields that I would like to substract. The problem is that the substraction is not very "natural".

    I mean, this will be an example resulting set:


    JoinId____|__QuantityA_____|__QuantityB
    1________|______10_______|______40
    1________|______32_______|______40
    1________|______5________|______40
    2________|______10_______|______15
    2________|______10_______|______15

    These two quantities are taken from different tables, lets say TableA and TableB... They are joined by the id I show (It is not a primary key)

    What I would like to do is have something like this:


    JoinId___|__QuantityA_____|__QuantityB___|___Result
    1_______|______10_______|______40_____|_____0_(We substracted 10 from 40)
    1_______|______32_______|______40_____|_____2_(We substract 32 from remaining 30)
    1_______|______5________|______40_____|_____5_(We substract 5 from remaining 0)
    2_______|______10_______|______15_____|_____0_(We substracted 10 from 15)
    2_______|______10_______|______15_____|_____5_(We substract 10 from remaining 5)

    It looks complicated but what I want to do is a substraction with every member of the same JoinId. The remaining would be substracted to the next one.

    Example:


    JoinId___|__QuantityA__|__QuantityB
    1_______|______10____|______40____(40 - 10 = 30 so we will substract below)
    1_______|______32____|______30____(30 - 32 = -2 so we won't substract below)
    1_______|______5_____|______0_____

    2_______|______10____|______15____(15 - 10 = 5 so we will substract below)
    2_______|______10____|______5_____(5 - 10 = -5 so we won't substract below)


    And so the results of this would be like this:

    JoinId___|__QuantityA__|__QuantityB__|_Result
    1_______|______10____|______40____|___0
    1_______|______32____|______30____|___2
    1_______|______5_____|______0_____|___5

    2_______|______10____|______15____|___0
    2_______|______10____|______5_____|___5


    I hope I am explaining myself... Please let me know if I didn't. I think (I am not sure) it could be done using sub-queries and GROUP BY clauses... But I am not quite sure how to do it. Any help would be appreciated!

    Thanks in advance!
    Last edited by Tec-Nico; Dec 30th, 2004 at 06:55 PM. Reason: "Tables" looked weird...
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width