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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:55:55+00:00 2026-06-11T14:55:55+00:00

I am trying to have the results from two columns in table1 subtracted from

  • 0

I am trying to have the results from two columns in table1 subtracted from one another and add the total from table2 should the data exist in that table. Not always will there be data in table2 so if there is nothing there I need to use “0”. Here is what I have so far which returns the wrong amount when there IS data in table2.

SELECT 
    CONVERT(CHAR(10),table1.PostingDate, 120) AS business_date, 
    table1.Location AS store_number, 
    (CASE WHEN COUNT(table2.Document) > 0 THEN 
          SUM(table1.Total - table1.TipAmount + table2.Total)
          Else 
                SUM(table1.Total-table1.TipAmount) 
    END) AS net_sales_ttl
FROM table1 
    left join table2 
        on CONVERT(CHAR(10),table1.PostingDate, 120) = CONVERT(CHAR(10),table2.PostingDate, 120)
WHERE table1.PostingDate between '2012-09-09' and '2012-09-16'
GROUP BY CONVERT(CHAR(10),table1.PostingDate, 120), table1.Location

Here is the results:

business_date   store_number    net_sales_ttl
2012-09-09  xxx         1699.61
2012-09-10  xxx             923.56
2012-09-11  xxx             1230.93  <--This should be 1399.93
2012-09-12  xxx             874.98
2012-09-13  xxx             1342.21
2012-09-14  xxx             1609.6
2012-09-15  xxx             2324.31

For some reason the query is not doing the math correctly and returning the wrong values. The only day that table2 has a value is 09-11-12 and that amount is only -1.00. It is giving me 1230.93 which is -169 from the correct value. I don’t know where the -169 is coming from when it should be -1.00. Original amount is 1400.93 in table1 and table2 should be subtracted from that which is -1.00 giving a result of 1399.93.

    Sample data:
    table1 has date, location, and sales
    09-09 1111 5.00
    09-10 1111 3.00
    09-11 1111 7.00
    09-12 1111 10.00
    table2 has refunds
    09-11 1111 -1.00

    Return set would look like this:
    09-09 1111 5.00
    09-10 1111 3.00
    09-11 1111 6.00   <--Reflecting the refund from table2
    09-12 1111 10.00
  • 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-11T14:55:56+00:00Added an answer on June 11, 2026 at 2:55 pm


    try

    SELECT  
        CONVERT(CHAR(10),table1.PostingDate, 120) AS business_date,  
        table1.Location AS store_number,  
        SUM (table1.Total - table1.TipAmount + isnull(table2.Total,0))
    FROM table1   
        left join table2  
        on CONVERT(CHAR(10),table1.PostingDate, 120) = CONVERT(CHAR(10),table2.PostingDate, 120)  
    WHERE table1.PostingDate between '2012-09-09' and '2012-09-16'  
    GROUP BY CONVERT(CHAR(10),table1.PostingDate, 120), table1.Location 
    

    Also, some sample data would help – the totals alone can’t help

    Based on your sample data, try this.

    Select date, location, sum(sales)
    from 
    (
        select date, location, sales from table1 
        union
        select date, location, refunds from table2 
    ) v
    group by date, location
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, and I'm trying to look up values from one to
Im trying to write the results from a SProc to a textfile. I have
I have been trying to use array_unique to remove duplicates from the search results
I have one table: dbo.Product Two columns: SKU Name --- ---- 100A-B (F102397) 1
I have a two tables, one that contains basic resource info, with columns and
I trying to query a database to find relevant results between two columns in
I'm trying to get data from one database and put it in the format
I have two processes that work with data in the same table. One process
I have two tables with the following columns: table1: id, agent_name, ticket_id, category, date_logged
Trying to run my program in FreeBSD OS, I have the following results: $

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.