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

  • Home
  • SEARCH
  • 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 8424259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:56:23+00:00 2026-06-10T03:56:23+00:00

select id, amount – (select sum(amount) as amount from table2 where column = ‘value’

  • 0
select 
    id, amount - (select sum(amount) as amount 
                  from table2 
                  where column = 'value' and table1.id = table2.id 
                  group by table2.id) as amount
from 
    table1 
order by 
    table1.id

The result is all values from table1 – amount from table2 except the table1.id‘s that are not in table2.id‘s they get null value.

So it’s all good except that I have the null values because I need these to be the table1.amount value

Also tried stuff like this but still not working

select 
     id, amount - isnull((select sum(amount) as amount 
                          from table2 
                          where column = 'value' and table1.id = table2.id 
                          group by table2.id), table1.amount) as amount
from 
     table1 
order by 
     table1.id

Then I get 0.00 but the table1.id‘s that have null in result set do have a real amount value in table1.amount

Overview of what I need

table 1   values    (1,12 ; 2,27 ; 3,9) table 2   values    (1,9 ; 3,12) result table (1,3 ; 2,27 ; 3,-3)

so I need table1 – values from table 2 to get result table

  • 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-06-10T03:56:24+00:00Added an answer on June 10, 2026 at 3:56 am

    better do this

    select
    t1.id, t1.amount - isnull(t2.amountTable2,0) as 'amount'
    from table1 T1
    left join (select id, sum(ammunt) as 'amountTable2' from table2 group by Id) T2
           on t1.id = t2.id
    order by t1.id
    

    this will get the total Sum of each id in table2
    then will join by id, and do the difference.
    If there is no id in table2, will use 0

    That answer consider (I thougth that at first) that t2.Id could be repeated
    If it’s unique, remove the group by

    select
        t1.id, t1.amount - isnull(t2.amount,0) as 'Totalamount'
        from table1 T1
        left join table2 T2
               on t1.id = t2.id
        order by t1.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If SELECT SUM(amount) FROM transactions ORDER BY order LIMIT 0, 50 sums the amount
select tt.threshold_id from (select sum(amount) over (partition by tt.threshold_type order by tt.threshold_type ) amt
My SQL query: select *, (select sum(Amount) as TotalAmount from Payment where Customer.Id =
Ok, so I'm trying to select an amount of rows from a column that
SELECT table_1.id, (SELECT SUM(amount) FROM table_2 WHERE table_1.id = table_2.bid) AS sum_total FROM table_1
I'm looking for a way to optimise the following: SELECT (SELECT SUM(amount) FROM Txn_Log
I have this query in T-SQL 2008: SELECT a.Amount / ( SELECT SUM(b.Amount) FROM
When I run this query It works SELECT sum( amount ) AS balance FROM
$data = $mysqli->prepare(SELECT amount FROM items WHERE id=:id); echo 'forward1'; if(!$data->execute(array(':id' => $id))) die(error
<Select id=amount onchange=document.formName.submit(); updateCost();> <option id=0 value = 0> Select amount ... </option> <option

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.