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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:15:40+00:00 2026-06-05T02:15:40+00:00

I have a query that I’m trying to clean up: My original query is

  • 0

I have a query that I’m trying to clean up:

My original query is as follows:

SELECT h.userid, c.firstname, c.lastname, h.domain, h.nextduedate, h.domainstatus, 
    (SELECT COUNT(status) 
     FROM tblinvoices i, tblinvoiceitems ii
     WHERE i.userid =h.userid
            AND i.id = ii.invoiceid
        AND i.STATUS LIKE  'Unpaid'
        AND i.TOTAL = 80
        AND ii.description LIKE  'Hosted Domain%')
     AS invoice_count 
 FROM tblclients c, tblhosting h 
 WHERE h.userid=c.id AND c.status='Active' 
    AND c.id NOT IN (1,2,3,4,5)
    AND h.domain LIKE '%thisdomain.com' 
    AND h.nextduedate<='2012-06-06' 
 ORDER BY h.domainstatus, h.nextduedate DESC

It works fairly well, but it isn’t 100% accurate. I’m getting all of the users with a “nextduedate” that is older than today’s date, but, there are some users with Unpaid invoices with a NULL nextduedate (account is cancelled, but they still have unpaid invoices).

This query gives me 22 users, but if I run:

SELECT DISTINCT i.userid, COUNT(i.status) 
     FROM tblinvoices i, tblinvoiceitems ii
     WHERE i.id = ii.invoiceid
        AND i.STATUS LIKE  'Unpaid'
        AND i.TOTAL = 80
        AND ii.description LIKE  'Hosted Domain%')

Then I get 26 users (and about 8 missing invoices).

The Next Due Date is really to show how late they are, the main focus of this query is to show all of the users with Unpaid invoices. I tried to combined the second query with the first one several different ways, but I can’t figure it out, and I think I locked up my server.

Ideally my return row would look like this:

userid, firstname, lastname, domain, domainstatus, “duedate” (this would ideally be the date of the oldest invoice, I’m using nextduedate because it’s easy), invoicecount (the number of unpaid invoices)

FYI, the invoice date is in i.invoicedate

I would greatly appreciate help figuring out how to optimize this query!

  • 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-05T02:15:41+00:00Added an answer on June 5, 2026 at 2:15 am

    You may try to replace DISTINCT with GROUP BY

    SELECT h.userid, c.firstname, c.lastname, h.domain, h.nextduedate, h.domainstatus, count(i.status) AS invoice_count
    
    FROM tblclients c, tblhosting h 
    LEFT JOIN tblinvoices i ON i.userid =h.userid         
        AND i.STATUS LIKE  'Unpaid'
        AND i.TOTAL = 80
    LEFT JOIN tblinvoiceitems ii ON i.id = ii.invoiceid
        AND ii.description LIKE  'Hosted Domain%'
    
    
    WHERE h.userid=c.id AND c.status='Active' 
        AND c.id NOT IN (1,2,3,4,5)
        AND h.domain LIKE '%thisdomain.com' 
        AND h.nextduedate<='2012-06-06'
    GROUP BY h.userid 
    ORDER BY h.domainstatus, h.nextduedate DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that looks like this WITH CTE AS ( SELECT ROW_NUMBER()
I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, ... ( SELECT
I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I have a query that performs a UNION between two tables. I'm trying to
I have a query that looks like this: SELECT * FROM employees e LEFT
I have this query that spawns the following error: SELECT * FROM Quota WHERE
I have a query that looks like this: SELECT id, GROUP_CONCAT(type SEPARATOR ',') AS
I have query that goes like that : SELECT DISTINCT S.date1 AS date1, S.date2
I have a query that outputs address order data: SELECT ordernumber , article_description ,
I have a query that uses the IN clause. Here's a simplified version: 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.