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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:54:19+00:00 2026-05-30T04:54:19+00:00

Here is the thing, I have table like this First one ID1 ID2 ID3

  • 0

Here is the thing, I have table like this

First one

ID1 ID2 ID3

Second

ID1 ID2 ID3 month, value

What I need is to get values for three months for each combination of these 3 IDs, and I went this way:

SELECT a.ID1, a.ID2, a.ID3, b.value, c.value, d.value
FROM Table1 a

LEFT JOIN Table2 b
ON a.ID1 = b.ID1
AND a.ID2 = b.ID2
AND a.ID3 = b.ID3
WHERE month=2


LEFT JOIN Table2 c
ON a.ID1 = c.ID1
AND a.ID2 = c.ID2
AND a.ID3 = c.ID3
WHERE month=3


LEFT JOIN Table2 d
ON a.ID1 = d.ID1
AND a.ID2 = d.ID2
AND a.ID3 = d.ID3
WHERE month=4

And I got values multiplied.

Any idea why this is case, or any suggestions for other approaches?

Thanks.

  • 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-05-30T04:54:21+00:00Added an answer on May 30, 2026 at 4:54 am

    Assuming you want the next 3 months (next month and the two following months) based on today’s date, you can do this (you can also set @base to any specific date to the get the three following months):

    DECLARE @base DATETIME = GETDATE();
    
    ;WITH x AS
    (
        SELECT a.ID1, a.ID2, a.ID3, 
          m = DATEADD(MONTH, b.[Month]-1, RTRIM(b.[Year])+'0101'), b.value
        FROM dbo.Table1 AS a INNER JOIN dbo.Table2 AS b 
        ON a.ID1 = b.ID1 AND a.ID2 = b.ID2 AND a.ID3 = b.ID3
    ), y AS
    (
        SELECT ID1, ID2, ID3, m, md = DATEDIFF(MONTH, @base, m), value
        FROM x
        WHERE DATEDIFF(MONTH, @base, m) BETWEEN 1 AND 3
    )
    SELECT ID1, ID2, ID3, 
        BValue = SUM(CASE WHEN md = 1 THEN value END),
        CValue = SUM(CASE WHEN md = 2 THEN value END),
        DValue = SUM(CASE WHEN md = 3 THEN value END)
    FROM y 
    GROUP BY ID1, ID2, ID3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the thing. Right now I have this e-commerce web site where people
The first thing i would like to say is that this is not exactly
Feel like this should be something easy that I'm missing. I have a table
I have been on this for hours on end. When I get one thing
I have two <select> one is category and the second is subcategory. here is
Here's the thing: I have two applications, written in C++ and running on two
Here is the thing. I currently have a tabBar controller, with several navigation controllers
So I saw this post here and read it and it seems like bulk
In this code, I have an Entity Framework 4 model with a single Thing
Here is my problem. I have a plist called myPlist and it looks like

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.