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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:47:07+00:00 2026-06-17T16:47:07+00:00

I have accounts which have category associated with it & orders table where order

  • 0

I have accounts which have category associated with it & orders table where order can be either debt or credit based on “order_type” field & a payment table which stores the date of payment along with the amount (no negative numbers used for debt or credits).

currently I am using something like this:

SELECT cat, SUM(p.amount) balance
FROM accounts a 
LEFT JOIN orders o ON a.id = o.account_id
LEFT JOIN payments p ON o.id = p.order_id
WHERE YEAR(p.date) = 2012
GROUP BY a.cat

The first issue:

This would count all as paid-in money which is wrong, I’ve tried to use

sum(IF(o.order_type = 1, p.amount, p.amount * -1 ))

which failed to give me correct result as all amounts were negative.

Second issue:

It won’t show categories where no payments were made, the limiting factor here is the date which I need to make sure that I am counting payments on 2012 only but I also want to show all categories with zeros if no payments were made.

Am I approaching it the WRONG way?

If anyone knows a solution for either issues or both, please provide your solution on this.

  • 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-17T16:47:07+00:00Added an answer on June 17, 2026 at 4:47 pm
    SELECT cat, SUM(p.amount) balance
    FROM accounts a 
    LEFT JOIN orders o
      ON a.id = o.account_id
         AND o.order_type = 1
    LEFT JOIN payments p
      ON o.id = p.order_id
         AND YEAR(p.date) = 2012
    GROUP BY a.cat
    

    The year check has to be in the JOIN condition, not WHERE, so it won’t exclude the categories with no payments.

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

Sidebar

Related Questions

In our system we have accounts which contain items. An item is always associated
I'm currently developing a Django site in which users can have multiple 'accounts', so
I have a site 'accounts' table which contains account details for all our users.
I have verious accounts which need to be kept track of. These accounts are
I have a program which reads Inbox messages from email accounts, as the title
I have two tables: Accounts , which contains the information of accounts that a
say I have this on my routes.rb: resources :accounts which gives me, among other
I'm looking to clean up permissions for user accounts which have been deleted. I
I have a SQL Server (2000) with a number of accounts which I think
I have accounts which they have some settings attributes. As settings there are value

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.