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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:03:42+00:00 2026-06-13T01:03:42+00:00

I have a T-SQL query that I need to total out the counts for

  • 0

I have a T-SQL query that I need to total out the counts for the CASE statements.

I tried to add a UNION but I am getting an error:

All queries in an SQL statement containing a UNION operator must have
an equal number of expressions in their target lists.

Any ideas? Thanks.

Query:

SELECT 
   CustomerID, Name, DueDate, 
   CASE WHEN DATEDIFF(Month, PaymentDate, DueDate) >= 1 
            THEN PaymentAmount ELSE 0 
   END AS [Early],
   CASE WHEN DATEDIFF(Month, PaymentDate, DueDate) >=0 
            THEN PaymentAmount ELSE 0 
   END AS [On Time],
   CASE WHEN DATEDIFF(Month, PaymentDate, DueDate) = -1 
            THEN PaymentAmount ELSE 0 
   END AS [Late]
FROM 
    Customers 
WHERE 
    DATEDIFF(MONTH, PaymentDate,DueDate), GetDate()) = 1
    AND PaymentAmount= DuesAmount

UNION

SELECT 
    '-Total', '', CustomerID, Name, DueDate,
    SUM(CASE WHEN DATEDIFF(Month, PaymentDate, DueDate) >= 1 
               THEN PaymentAmount ELSE 0 END) AS [Early],
    SUM(CASE WHEN DATEDIFF(Month, PaymentDate, DueDate) >= 0 
               THEN PaymentAmount ELSE 0 END) AS [On Time],
    SUM(CASE WHEN DATEDIFF(Month, PaymentDate, DueDate) = -1 
               THEN PaymentAmount ELSE 0 END) AS [Late]
FROM 
    Customers
WHERE 
    DATEDIFF(MONTH, PaymentDate,DueDate), GetDate()) = 1
    AND PaymentAmount = DuesAmount 
  • 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-13T01:03:43+00:00Added an answer on June 13, 2026 at 1:03 am

    The error says, “All queries in a SQL statement containing a UNION operator must have an equal number of expressions in their target lists.” What this means is that each SELECT must return the same number of expressions.

    In the code sample you gave us, the first SELECT statement returns 6 expressions, while the second one returns 8:

    SELECT CustomerID,                -- 1
           Name,                      -- 2
           DueDate,                   -- 3
           CASE ... END AS [Early],   -- 4
           CASE ... END AS [On Time], -- 5
           CASE ... END AS [Late]     -- 6
    ...
    UNION
    SELECT '-Total',                       -- 1
           '',                             -- 2
           CustomerID,                     -- 3
           Name,                           -- 4
           DueDate,                        -- 5
           SUM(CASE ... END) AS [Early],   -- 6
           SUM(CASE ... END) AS [On Time], -- 7
           SUM(CASE ... END) AS [Late]     -- 8
    ...
    

    See how the first SELECT returns 6 expressions, but the second returns 8? In a UNION, all the SELECT statements must return the same number of expressions.

    You can return NULL in the first query for columns that don’t have a match in the second, if necessary. For example:

    SELECT NULL as [RowType],         -- 1
           NULL as [Padding],         -- 2
           CustomerID,                -- 3
           Name,                      -- 4
           DueDate,                   -- 5
           CASE ... END AS [Early],   -- 6
           CASE ... END AS [On Time], -- 7
           CASE ... END AS [Late]     -- 8
    ...
    UNION
    SELECT '-Total',                       -- 1
           '',                             -- 2
           CustomerID,                     -- 3
           Name,                           -- 4
           DueDate,                        -- 5
           SUM(CASE ... END) AS [Early],   -- 6
           SUM(CASE ... END) AS [On Time], -- 7
           SUM(CASE ... END) AS [Late]     -- 8
    ...
    

    Also, note that you don’t have a comma after the DueDate column.

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

Sidebar

Related Questions

I currently have a SQL query that returns a number of fields. I need
I need to be able to have an SQL query that searches my database
Using Microsoft SQL server 2008 I have a query that is in need of
I have a SQL query that I'm trying to write, but I'm not quite
I have this SQL Query that pulls data from 3 tables. I am unable
I have a SQL query that returns a Datatable: var routesTable = _dbhelper.Select(SELECT [RouteId],[UserId],[SourceName],[CreationTime]
I have a SQL query that I'm trying to debug. It works fine for
I have an SQL query that returns all companies records ignore the ones with
Suppose I have a SQL Query that isn't really nice. The SQL Query has
i have a huge sql query that is attached to the rowsource of a

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.