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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:37:38+00:00 2026-05-11T09:37:38+00:00

I have a search that is very slowly done by finding the primary records

  • 0

I have a search that is very slowly done by finding the primary records then looping in a thread to sum related transactions. I’m trying to make it work in a single statement, have come close but still have records which are alternating debit and credits.

I can’t work out how to pull the debit and credit rows up into columns so there is one row of results per date and jobkey.

SELECT j.dtmInvoicedOn, j.strJobKey, c.strCustName, strTransType,       SUM(r.dblTransactionAmount) AS SUM_dblTotalCharge       FROM tbljobs AS j      INNER JOIN tblreceivledger AS r ON j.strJobKey = r.strJobKey       INNER JOIN tblcustomers AS c ON j.intCustomerID = c.intCustomerID       WHERE c.strCustomerName = 'Acme Runners Inc'      GROUP BY j.strJobKey, c.strCustName, strTransType      ORDER BY dtmInvoicedOn, strJobKey; 

produces output like this, with nearly alternating debit and credit sums

+----------------+---------------+------------------+--------------------+--------------------+ | dtmInvoicedOn  | strJobKey     | strCustomerName  | strTransactionType | SUM_dblTotalCharge | +----------------+---------------+------------------+--------------------+--------------------+ | 2008-07-03     | 270876-1      | Acme Runners Inc | credit             |           -5531.52 |  | 2008-07-11     | 270880-1      | Acme Runners Inc | debit              |            5058.54 |  | 2008-07-11     | 270880-1      | Acme Runners Inc | credit             |           -5058.54 |  | 2008-07-18     | 271468-1      | Acme Runners Inc | debit              |            5290.17 |  | 2008-07-18     | 271468-1      | Acme Runners Inc | credit             |           -5290.17 |  | 2008-11-07     | 286049-1      | Acme Runners Inc | debit              |            5230.44 |  | 2008-11-14     | 286051-1      | Acme Runners Inc | debit              |            5375.14 |  | 2008-11-21     | 286107-1      | Acme Runners Inc | debit              |            5572.33 |  | 2008-11-28     | 286112-1      | Acme Runners Inc | debit              |            5123.42 |  

so I want it to look like:

+----------------+---------------+------------------+----------+----------+ | dtmInvoicedOn  | strJobKey     | strCustomerName  |   credit |    debit | +----------------+---------------+------------------+----------+----------+ | 2008-07-03     | 270876-1      | Acme Runners Inc | -5531.52 |        0 | | 2008-07-11     | 270880-1      | Acme Runners Inc | -5058.54 |  5058.54 |  | 2008-07-18     | 271468-1      | Acme Runners Inc | -5290.17 |  5290.17 |  | 2008-11-07     | 286049-1      | Acme Runners Inc |        0 |  5230.44 |  | 2008-11-14     | 286051-1      | Acme Runners Inc |        0 |  5375.14 |  | 2008-11-21     | 286107-1      | Acme Runners Inc |        0 |  5572.33 |  | 2008-11-28     | 286112-1      | Acme Runners Inc          0 |  5123.42 |  

Note that the server is currently running mysql but will migrate to postgres and sqlite later.

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. 2026-05-11T09:37:39+00:00Added an answer on May 11, 2026 at 9:37 am

    this should do the job:

    SELECT j.dtmInvoicedOn, j.strJobKey, c.strCustName, strTransType,       SUM(CASE WHEN strTransType='credit' THEN r.dblTransactionAmount ELSE 0 END) AS SUM_CREDIT,      SUM(CASE WHEN strTransType='debit' THEN r.dblTransactionAmount ELSE 0 END) AS SUM_DEBIT FROM tbljobs AS j      INNER JOIN tblreceivledger AS r ON j.strJobKey = r.strJobKey       INNER JOIN tblcustomers AS c ON j.intCustomerID = c.intCustomerID  WHERE c.strCustomerName = 'Acme Runners Inc' GROUP BY j.strJobKey, c.strCustName ORDER BY dtmInvoicedOn, strJobKey; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have search that has a lot of different options, then url becomes
I have a search that returns JSON, which I then transform into a HTML
I need some help. Right now i have done a file search that will
I have a very simple search query that looks like this select * from
My website has a search procedure that runs very slowly. One thing that slows
I have a search page that if there is results in the list it
I have a search method that takes in a user-entered string, splits it at
I have a search engine that searches albums. For each music album, I have
I have a search functionality that obtains data from an InnoDB table ( utf8_spanish_ci
the div on website below that have search at the bottom and it always

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.