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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:23:45+00:00 2026-06-05T14:23:45+00:00

Consider the following SQL statement: DECLARE @CustomerId INT = 48; DECLARE @CurrentYear INT =

  • 0

Consider the following SQL statement:

DECLARE @CustomerId INT = 48;
DECLARE @CurrentYear INT = DATEPART(YEAR, GETDATE());

SELECT
    YEAR(o.OrderDate) AS Year,
    0 AS Month,
    SUM(o.GrandTotal) AS GrandTotal
FROM [Order] o
WHERE
    o.CustomerId = @CustomerId AND
    o.OrderTypeId = 4 AND
    o.IsVoid = 0 AND
    YEAR(o.OrderDate) BETWEEN @CurrentYear - 2 AND @CurrentYear
GROUP BY
    YEAR(o.OrderDate)

which correctly produces the result:

2012, 0, 89.00
2011, 0, 230.00
2010, 0, 450.0

However, if another customer has placed an order just for year 2011, I need to have those 2 other rows generated (albeit with 0 values):

2011, 0, 230.00
2012, 0, 0
2010, 0, 0

How am I suppose to do this correctly? Please note that the report is going to be generated for the last 3 years.

4th UPDATE
I modified the code as suggested:

DECLARE @CustomerId INT = 48;
DECLARE @CurrentYear INT = YEAR(GETDATE());

DECLARE @years TABLE (
    yr INT
);
INSERT INTO @years VALUES
    (@CurrentYear),
    (@CurrentYear - 1),
    (@CurrentYear - 2)

This produces a result set as follows (I’ve checked that and made sure that this is the case).

2012
2011
2010

Then I join the tables (RIGHT JOIN) as follows:

SELECT
    y.yr AS Year,
    0 AS Month,
    SUM(ISNULL(o.GrandTotal, 0)) AS GrandTotal
FROM [Order] o
RIGHT JOIN @years y ON y.yr = YEAR(o.OrderDate)
WHERE
    o.CustomerId = @CustomerId AND
    o.OrderTypeId = 4 AND
    o.IsVoid = 0
GROUP BY
    y.yr

But, consider that the customer hasn’t placed an order yet, so, this needs to produce 3 rows with ZERO values. However, none of those solutions suggested does this.

FINAL UPDATE [SOLVED]:
The where clause prevented those rows from being in the final result set. So, as Darvin suggested, I just replaced the WHERE clause with AND and the problem is gone.

SELECT
    y.yr AS Year,
    0 AS Month,
    SUM(ISNULL(o.GrandTotal, 0)) AS GrandTotal
FROM [Order] o
RIGHT JOIN @years y ON y.yr = YEAR(o.OrderDate) AND
    o.CustomerId = @CustomerId AND
    o.OrderTypeId = 4 AND
    o.IsVoid = 0
GROUP BY
    y.yr
  • 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-05T14:23:48+00:00Added an answer on June 5, 2026 at 2:23 pm

    How about the following:

    DECLARE @CustomerId INT = 48; 
    DECLARE @CurrentYear INT = DATEPART(YEAR, GETDATE());  
    
    ;with years as 
    ( 
    SELECT YEAR(GETDATE()) as yr 
    UNION ALL SELECT YEAR(DATEADD(YY,-1,GETDATE())) 
    UNION ALL SELECT YEAR(DATEADD(YY,-2,GETDATE())) 
    ) 
    SELECT      
    years.yr 
    ,0 AS Month 
    ,SUM(ISNULL(o.GrandTotal,0)) AS GrandTotal  
    FROM years 
    LEFT OUTER JOIN Order o  
    ON YEAR(o.OrderDate) = years.yr 
    AND o.customerid = @customerid
    AND     o.OrderTypeId = 4  
    AND     o.IsVoid = 0  
    GROUP BY      
    years.yr 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following SQL statement: SELECT invoice.customer_name, invoice.customer_id, invoice.shop_location, sum(invoice.cost) FROM invoice GROUP BY
Can i apply SUM() within an ISNULL() .... Consider my following sql server select
Consider the following SQL (SQL Server 2008) statement: WITH MyResult AS ( SELECT Name,
Consider the following code. DECLARE @sql VARCHAR(MAX) SELECT @sql = 'SELECT * FROM caseinformation'
Consider the following 3 standard statements $queryString = SOME SQL SELECT QUERY; $queryResult =
Consider the following sql server query , DECLARE @Table TABLE( Wages FLOAT ) INSERT
Please consider the following SQL Server table and stored procedure. create table customers(cusnum int,
Consider the following Transact sql. DECLARE @table TABLE(val VARCHAR(255) NULL) INSERT INTO @table (val)
Consider the following code: Dim sql = SELECT * FROM MyTable WHERE value1 =
Consider the following query: INSERT INTO statement_line_items SELECT count(*)::integer as clicks, sum(amount_cents)::integer as amount_cents,

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.