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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:27:51+00:00 2026-06-01T00:27:51+00:00

i have been banging my head for the solution for nearly 14 hours and

  • 0

i have been banging my head for the solution for nearly 14 hours and still not able to find out a solution. I will try and make my problem as clear as i can. Please help me out

i have 2 tables

  1. cash
  2. mas

the structure of the tables is like

mas table

id – int

dateofissue – datetime

amount – float


cash table

id – int

date – datetime

amount – float


now what i want is to make a monthly report which tells the date wise sum total of the amounts of cash and from the master table

the structure of the report will be like


date—————— master amount ————— cash amount

2/3/2012————– 2000 ———————— 1000

3/3/2102————– 4000 ———————— 200

…

….


the total of amount from the mas table and the total of the amount from the cash table is grouped according to the dateofissue from mas table and date from cash table.

i am using a sql query like

     SELECT     sum(cash.amount) as CashAmount,  sum ( mas.amount) AS DraftAmount, mas.dateofissue
FROM         mas FULL OUTER JOIN
            cash ON cash.date = mas.dateofissue   
where mas.dateofissue >= '2012-03-01 00:00:00.000' and mas.dateofissue <= '2012-03-31 00:00:00.000' and 
cash.date >= '2012-03-01 00:00:00.000' and cash.date <= '2012-03-31 00:00:00.000'
group by mas.dateofissue

now the query works but double or triples the amounts which were aggregated
i searched for a solution and someone told to use distinct with sum
as

sum(distinct mas.amount)

but the answers were not coming correctly.

I know i am not good with explaining problems but please help me out here.

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-06-01T00:27:53+00:00Added an answer on June 1, 2026 at 12:27 am

    It’s not SUM() that’s causing the doubling, it’s the JOIN. SUM() is done after you’re done your JOIN, and your JOIN appears to be matching one record from one talbe to two records from the other table.

    Either, you need to de-duplicate your tables to prevent the 1:2 matching in your join, or pre-aggregate your tables…

    Pre-process option:

    SELECT
      COALESCE(mas.dateofissue, cash.date) AS date,
      cash.amount                          AS CashAmount,
      mas.amount                           AS DraftAmount
    FROM
      (SELECT dateofissue, SUM(amount) as amount FROM mas GROUP BY dateofissue) AS mas
    FULL OUTER JOIN
      (SELECT date, SUM(amount) as amount FROM cash GROUP BY date) AS cash
         ON cash.date = mas.dateofissue
    WHERE
          (mas.dateofissue >= '2012-03-01 00:00:00.000' OR mas.dateofissue IS NULL)
      and (mas.dateofissue <= '2012-03-31 00:00:00.000' OR mas.dateofissue IS NULL)
      and (cash.date       >= '2012-03-01 00:00:00.000' OR cash.date       IS NULL)
      and (cash.date       <= '2012-03-31 00:00:00.000' OR cash.date       IS NULL)
    

    Identify which table has the duplicates:

    SELECT dateofissue, COUNT(*) FROM mas  GROUP BY dateofissue HAVING COUNT(*) > 1
    SELECT date,        COUNT(*) FROM cash GROUP BY date        HAVING COUNT(*) > 1
    

    EDIT

    I’ve also noticed that you have a FULL OUTER JOIN and a filter in the WHERE clause; this forces it back into being an INNER JOIN.

    So, I’ve changed the above query to actually work as a FULL OUTER JOIN, there are other ways to do it in your case, such as doing the filter in the sub-queries, but the changes I made should help see how to filter a FULL OUTER JOIN 🙂

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

Sidebar

Related Questions

I have been banging my head for hours trying to figure out why this
I have been banging my head trying to figure out what is going wrong.
I have been banging my head on this one for hours and I am
I have been banging my head on this for a few hours and google
I have been banging my head against the wall trying to figure out this
I have been banging my head on this one for too long. I have
I have been banging my head on and on, i cant seem to work
I have been banging my head against the wall with this odd behaviour on
I have been banging my head on a wall with this one. I need
I am having a problem and have been banging my head aginst a wall...

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.