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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:54:09+00:00 2026-06-12T15:54:09+00:00

I have the following db schema (i’m only mentioning the columns for each table

  • 0

I have the following db schema (i’m only mentioning the columns for each table necessary for the query in mind):

bills
-------
id
amount (decimal)
collection_case_id


collection_case
------------------
id


payments
----------
id
bill_id
amount (decimal)
type (char(2)) - "CH" stands for check payment, field used for polymorphism 

check_payments
---------------
payment_id

For a set of collection cases, I need to get the total interest earned from check payments.
I am currently doing these calculations in memory after retrieving all the bill records for my set of collection case criteria. For each bill I simply add the sum of checks received – bill amount if sum of checks received is greater than bill amount.

for instance my total received query looks like this (collection cases 1 & 2):

select sum(payment.amount) as received 
from bills bill
cross join payments payment inner join check_payments checkpayment 
on 
payment.id=checkpayment.payment_id 
where payment.type='CH'
 and payment.bill_id=bill.id 
 and (bill.collection_case_id in (1 , 2))
  • 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-12T15:54:10+00:00Added an answer on June 12, 2026 at 3:54 pm

    Not really sure if this is what you meant.

    select IF(sum(payment.amount) > bill.amount, sum(payment.amount)-bill.amount, 0) as interest  
    from bills bill
    cross join payments payment inner join check_payments checkpayment 
    on 
    payment.id=checkpayment.payment_id 
    where payment.type='CH'
     and payment.bill_id=bill.id 
     and (bill.collection_case_id in (1 , 2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have a bunch of coordinates in my table grid with the following schema
I have the following schema: Database: test. Table: per_login_user, Field: username (PK), password Database:
I have the following schema definition: Usuario: columns: empresa_id: { type: BIGINT, notnull: true
I do have the following schema.yaml: Artikel: connection: doctrine tableName: artikel columns: id: type:
Assuming I have the following schema table A: ID int primary key value varchar(255)
I have the following schema.yml file: Page: actAs: I18n: fields: [name,html,urlShortDesc] columns: name: string
I have the following schema: TABLE bands -> band_id -> property1 -> property2 ->
I have the following schema (generated from an existing table with the schema module
I have the following schema, and would like to do a query that returns

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.