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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:42:59+00:00 2026-06-17T10:42:59+00:00

I am having a struggle with displaying data properly on the page. I am

  • 0

I am having a struggle with displaying data properly on the page. I am running a query for a billing report that searches all outstanding bills and I want to display them on a single page with the outstanding balance being displayed by how old the balance is. So if client ABC has 10 invoices over the past year and some of them have outstanding balances, I would like to display those balances in columns of 0-30 / 30-60 / 60-90 / and 90+ days old. Here is what I have…

    $sql17 = "SELECT $mysql_billing.primary_key, $mysql_billing.a_name, $mysql_billing.invoicedate, $mysql_billing.finaltotal, $mysql_billing_dates.paidtotal,
 ($mysql_billing.finaltotal - $mysql_billing_dates.paidtotal) AS total
 FROM
   (SELECT $mysql_billing.primary_key, $mysql_billing.a_name, $mysql_billing.login_username, $mysql_billing.invoicedate,
   SUM($mysql_billing.custotal) AS finaltotal 
   FROM $mysql_billing where $today >= invoicedate AND $start_search_prev180_date <= invoicedate AND custotal != payments GROUP BY login_username) $mysql_billing 
 LEFT JOIN 
   (SELECT $mysql_billing_dates.id, $mysql_billing_dates.username, 
   SUM($mysql_billing_dates.amount) AS paidtotal 
   FROM $mysql_billing_dates where complete != 'yes' GROUP BY username) $mysql_billing_dates 
 ON ($mysql_billing.login_username = $mysql_billing_dates.username) 
 GROUP BY a_name ORDER BY a_name ASC";

this seems to print out the results but how do you get the results into different columns based off of more than 1 date variable? Do I have to do 4 separate queries?

  • 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-17T10:43:00+00:00Added an answer on June 17, 2026 at 10:43 am

    Your SQL Fiddle was close, but a few of your joins were incorrect.

    SELECT 
      billing.a_name, 
      billing.finaltotal, 
      billing_dates.paidtotal,
      (billing.finaltotal - billing_dates.paidtotal) AS total,
       billing.TimePeriod
    FROM
       (SELECT 
          a_name, 
          login_username, 
          SUM(custotal) AS finaltotal,
          CASE 
            WHEN invoicedate > DATE_SUB(CURDATE(),INTERVAL 30 DAY) THEN '30'
            WHEN invoicedate > DATE_SUB(CURDATE(),INTERVAL 60 DAY) THEN '60'
            WHEN invoicedate > DATE_SUB(CURDATE(),INTERVAL 90 DAY) THEN '90'
            ELSE '90+'
          END AS TimePeriod
    
        FROM billing 
        where CURDATE() >= invoicedate 
          AND CAST(20120601 AS DATETIME) <= invoicedate 
        GROUP BY login_username) billing 
     LEFT JOIN 
       (SELECT 
          username, 
          SUM(amount) AS paidtotal 
        FROM billing_dates
        GROUP BY username) billing_dates 
       ON (billing.a_name = billing_dates.username) 
    ORDER BY a_name ASC;
    

    Here is the updated Fiddle.

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

Sidebar

Related Questions

I've figured out CUBE as just generating all the permutations, but I am having
I'm having a hell of a time getting WCF Data Services to work within
I have been having constant struggle with Visual Studio debugger and finally got fed
I struggle finding the solution for having an incoming POST request hit my service
Having a bit of struggle with Unicode file names in OS X and Python.
I am working on a PowerPivot report that has the following tables/relationships: FactTable: SponsorId
We are having some issues with our data layer when large datasets are returned
I'm trying to modify a script that stores my session data in a database
I'm having a little struggle on this one and would appreciate some help. In
I am having a bit of a struggle with Expressions inside SSIS Derived Columns.

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.