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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:07:14+00:00 2026-06-12T13:07:14+00:00

I have 1 table with similar data: CustomerID | ProjectID | DateListed | DateCompleted

  • 0

I have 1 table with similar data:

CustomerID | ProjectID | DateListed | DateCompleted
123456     | 045       | 07-29-2010 | 04-03-2011
123456     | 123       | 10-12-2011 | 11-30-2011
123456     | 157       | 12-12-2011 | 02-10-2012
123456     | 258       | 06-07-2011 | NULL

Basically, a customer contacts us, we get a project on our list, and we mark it completed when we’re done with it.

What I’m after is a simple (you’d think, at least) count of all projects, with expected output like below:

YEAR | TotalListed | TotalCompleted
2010 |       1     |        0
2011 |       3     |        2
2012 |       0     |        1

However, my query below – because of the join – isn’t showing 2012’s count, because there’s been no listed project for 2012. However, I can’t really reverse the query, as then 2010’s count wouldn’t show up (since nothing was completed in 2010).

I’m open to any suggestions, or tips like how to do this. I’ve pondered a temp table, is that the best way to go? I’m open to anything that gets me what I need!

(If the code looks familiar, ya’ll helped me get the subquery made! MySQL Subquery with main query data variable)

SELECT YEAR(p1.DateListed) AS YearListed, COUNT(p1.ProjectID) As Listed, PreQuery.Completed  
FROM(
    SELECT YEAR(DateCompleted) AS YearCompleted, COUNT(ProjectID) AS Completed 
    FROM projects 
    WHERE CustomerID = 123456 AND DateListed >= DATE_SUB(Now(), INTERVAL 5 YEAR) 
    GROUP BY YEAR(DateCompleted)
) PreQuery 
RIGHT OUTER JOIN projects p1 ON PreQuery.YearCompleted = YEAR(p1.DateListed)
WHERE CustomerID = 123456 AND DateListed >= DATE_SUB(Now(), INTERVAL 5 YEAR)
GROUP BY YearListed 
ORDER BY p1.DateListed
  • 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-12T13:07:18+00:00Added an answer on June 12, 2026 at 1:07 pm

    After reviewing your table, query, and expected results – I believe I have found a more-revised query to suit your needs. It is a fairly-full rewrite of your existing query though, but I’ve tested it with your given data and received the same results you want/expect:

    SELECT
        years.`year`,
        SUM(IF(YEAR(DateListed) = years.`year`, 1, 0)) AS TotalListed,
        SUM(IF(YEAR(DateCompleted) = years.`year`, 1, 0)) AS TotalCompleted
    FROM
        projects
        LEFT JOIN (
            SELECT DISTINCT `year` FROM (
                SELECT YEAR(DateListed) AS `year` FROM projects
                UNION SELECT YEAR(DateCompleted) AS `year` FROM projects WHERE DateCompleted IS NOT NULL
            ) as year_inner
        ) AS years
            ON YEAR(DateListed) = `year`
            OR YEAR(DateCompleted) = `year`
    WHERE
        CustomerID = 123456 AND DateListed >= DATE_SUB(Now(), INTERVAL 5 YEAR)
    GROUP BY
        years.`year`
    ORDER BY
        years.`year`
    

    To explain, we should start with the inner query (aliased as year_inner). It selects a full list of years in the DateListed and DateCompleted columns and then selects a DISTINCT list of those to create the years alias sub-query. This sub-query is used to get a full list of “years” that we want data for. Doing it this way, opposed to a sub-query with counts and groupings will allow you to only have to define the WHERE clause on the outermost query (though, if efficiency becomes an issue with thousands and thousands of records, you could always add a WHERE clause to the inner query too; or an index to the date columns).

    After we’ve built our inner queries, we join the projects table on the results with a LEFT JOIN for the DateListed or DateCompleted‘s YEAR() value – which will allow us to bring back null columns too!

    For the field selections, we use the year column from our inner query to assure that we get a full list of years to display. Then, we compare the current row’s DateListed & DateCompleted YEAR() value to the current year; if they’re equal, add 1 – else add 0. When we GROUP BY year, our SUM() will count all of the 1’s for that year for each column and give you the output you want (hopefully, of course =P).

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

Sidebar

Related Questions

I have a data.table object similar to this one library(data.table) c <- data.table(CO =
I have a table with a Name field containing data similar to the following:
I have a table in SQL Server 2000 with data similar to the following:
I have a table with data similar to the following: [ID], [State], [foo], [DateCreated],
I have a table with 692256 number of rows ,which has data similar to
I have a table with data similar to below: Group TimePoint Value 1 0
I have two similar tables table_a and table_b table_a is my current data and
I have a table similar to below in sql server 2005 date_column | field1
I have a table similar to this +--+-----+----+ |ID|Entry|Exit| +--+-----+----+ |18|32154|NULL| +--+-----+----+ |19|NULL |NULL|
I have a table similar to the following: id key str_val date_val num_val 1

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.