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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:00:39+00:00 2026-05-21T17:00:39+00:00

I am using following sql query… SELECT COALESCE(s.open_bal,0) AS open_balance , COALESCE(sum(i.amount),0) AS gross_fee

  • 0

I am using following sql query…

 SELECT COALESCE(s.open_bal,0)
        AS open_balance
      , COALESCE(sum(i.amount),0)
        AS gross_fee
      , COALESCE(sum(i.discount),0)
        AS discount 
      , COALESCE(sum(i.amount) - sum(i.discount),0)
        AS net_payable
      , COALESCE(SUM(r.reg_fee+r.tut_fee+r.other_fee),0)
        AS net_recieved
      , COALESCE(sum(i.amount), 0)
          - COALESCE(sum(i.discount), 0)
          - COALESCE(SUM(r.reg_fee+r.tut_fee+r.other_fee), 0)
        AS balance_due
      , b.name
        AS batch
      , b.id 
        AS batch_id
 FROM batches
  • 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-05-21T17:00:40+00:00Added an answer on May 21, 2026 at 5:00 pm

    Then just add IF() for a negative balance:

    COALESCE(sum(i.amount) - sum(i.discount), 0) 
    + IF(s.open_bal < 0, -s.open_bal, 0) 
    AS net_payable
    

    edit: Moving on to fix the query.

    going down from subscribers to both invoices and receipts is wrong idea itself. You’re joining receipts to invoices, and if there are more then 1 of any, the other side of join will be summed twice (N times, actually).

    Now, I’ll give a basic idea how the query should look. I’m not going into COALESCE() thing – I believe it’s unnecessary, as SUM() shouldn’t return NULLs.

    select
        b.name as batch_name,
        b.id as batch_id,
        s.open_bal as open_balance,
        invoices_sum.amount_sum as gross_fee,
        invoices_sum.discount_sum as discount,
        invoices_sum.amount_sum - invoices_sum.discount_sum
        + if(s.open_bal < 0, -s.open_bal, 0) as net_payable,
        recipts_sum.fee_sum as net_recieved
    from fm_batches b
    left join subscribers as s
        on batch = b.id
    left join (
      select student_id, sum(amount) as amount_sum, sum(discount) as discount_sum
      from invoices
      group by student_id
    ) as invoices_sum
        on s.id = invoices_sum.student_id
    left join (
      select student_id, sum(reg_fee + tut_fee + other_fee) as fee_sum
      from recipts
      group by student_id
    ) as recipts_sum
        on s.id = recipts_sum.student_id
    

    You get the idea.

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

Sidebar

Related Questions

I am using following SQL Query: SELECT `comment`.`id` AS `comment_id` , count( `comment_likes`.`comment_id` )
as example, I've the following SQL query ( using Access 2007 ) SELECT ID,
I have a SqlDependency set up using the following query: string sql = "SELECT
For instance, my query is like the following using SQL Server 2005: SELECT *
I'm using a SQL query that is similar to the following form: SELECT col1,
I am using following my sql query... SELECT p.id, p.title, p.description, p.game, p.icon, p.comments_allowed,
I am using the following SQL query: Select * from table1 as t1, table2
how can I simulate the following sql query using linq Select * From Resource
I'm using SQL Server 2008 and the following query to implement paged data retrieval
Hi i am using the following dynamic sql: declare @cmd nvarchar(4000) set @cmd= 'select

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.