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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:10:59+00:00 2026-06-15T23:10:59+00:00

PROBLEM: Need query to return the MONTH and YIELD for Each Year. For some

  • 0

PROBLEM: Need query to return the MONTH and YIELD for Each Year. For some reason, if the data is not found in a Month a.Month the query will not return the b.Month’s Yield. I need the query to return all monthly data regardless of whether or not a.Month contains a month with data in the same months as “b”.

THE FOLLOWING RESULT: SHOULD RETURN A VALUE FOR “MONTH 1 YIELD_1”. But it doesn’t… because “MONTH 1 YIELD_0” does NOT contain a value for month 1.

**DATA RESULTS WITH: LEFT OUTER JOIN:**
Month   Yield_1    Yield_0
2        11.44      14
3         NULL     3.21
4         NULL     14.24
7         NULL     10.36
8         NULL       0
9         NULL     -9.6
10        NULL     10.35
11        NULL      1.4
12        11.44    -1.18


**DATA RESULTS WITH RIGHT OUTER JOIN:**
Month   Yield_1    Yield_0
NULL     11.44      NULL
2        11.44       14
12       11.44     -1.18

QUERY:

SET @ID_CARTERA = 8;

select     
        a.Month Month,
        b.Monthly_Yield Yield_Year_1,
        a.Monthly_Yield Yield_Year_0

from
    ( select  
          LEFT(A.F_ANOMES, 4) Year,
          RIGHT(A.F_ANOMES, 2) Month,
          ROUND(A.POR_RENTABILIDAD, 2) Monthly_Yield

from      dr_rent_carteras_meses A
where     A.ID_CARTERA = @ID_CARTERA
And       A.IND_RENTABILIDAD = 1

And       LEFT(A.F_ANOMES, 4) = ( select MAX(left(F_ANOMES, 4 ) ) - 0 from dr_rent_carteras_meses where ID_CARTERA = @ID_CARTERA ) ) a


LEFT outer join 
        ( select  
          LEFT(A.F_ANOMES, 4) Year,
          RIGHT(A.F_ANOMES, 2) Month,
          ROUND(A.POR_RENTABILIDAD, 2) Monthly_Yield

from      dr_rent_carteras_meses A
where     A.ID_CARTERA = @ID_CARTERA
And       A.IND_RENTABILIDAD = 1
And       LEFT(A.F_ANOMES, 4) = ( select MAX(left(F_ANOMES, 4 ) ) - 1 from dr_rent_carteras_meses where ID_CARTERA = @ID_CARTERA ) ) b on ( a.Month = b.Month )

order by  month asc
  • 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-15T23:11:00+00:00Added an answer on June 15, 2026 at 11:11 pm

    Summary

    I don’t think you need either left, right, or full join for this specific query as a GROUP BY/CASEbased solution should work just fine and be at least twice as much faster.

    Problem Definition

    I found this question rather interesting as it seems to arise from a real life situation and I believe that in real life FULL JOIN is seldom if ever necessary. So to me the real question is not how to combine the data from left and right joins but rather why is a full join needed in the first place?

    Besides, simple replacing LEFT with FULL would not be enough as that would lead to

    1. Duplicate rows for the same months;
    2. NULL months for when the month is not available in the latest year.

    So I decided to go ahead and decipher the query.

    Table and Data Setup

    Unfortunately, @smileyseven didn’t provide table definitions or insert statements, so I had to work them out from the query and sample data. Based on the query syntax I’m assuming that SQL Server was used so that’s what I use as well.

    Here’s my guess at how the table looks:

    CREATE TABLE dr_rent_carteras_meses 
    (
        F_ANOMES varchar(6) NOT NULL PRIMARY KEY,
        POR_RENTABILIDAD float NOT NULL,
        -- These columns are  irrelevant 
        ID_CARTERA INT NOT NULL DEFAULT(8),
        IND_RENTABILIDAD INT DEFAULT(1) NOT NULL
    )   
    

    The important point ist hat F_ANOMES is probably the key column as otherwise we would have to expect multiple rows for each month in the query output and that seems unlikely.

    The following insert statements should produce the sample data:

    INSERT dr_rent_carteras_meses (F_ANOMES, POR_RENTABILIDAD) VALUES 
    ('201202', 14),
    ('201203', 3.21),
    ('201204', 14.24),
    ('201207', 10.36),
    ('201208', 0),
    ('201209', -9.6),
    ('201210', 10.35),
    ('201211', 1.4),
    ('201212', -1.18),
    ('201101', 11.44),
    ('201102', 11.44),
    ('201112', 11.44)
    

    Solution

    The first thing to note is that we don’t really need to calculate the maximum year two times, so we’ll start with this:

    declare @Max_Year int
    select 
        @Max_Year = MAX(left(F_ANOMES, 4))
    from dr_rent_carteras_meses 
    where ID_CARTERA = @ID_CARTERA
    

    The a and b tables are virtually same, so we could probably reuse them:

    ;with 
    Monthly_Yield_CTE as
    ( 
        select  
            LEFT(F_ANOMES, 4) Year,
            RIGHT(F_ANOMES, 2) Month,
            ROUND(POR_RENTABILIDAD, 2) Monthly_Yield
        from      dr_rent_carteras_meses
        where     ID_CARTERA = @ID_CARTERA
        and       IND_RENTABILIDAD = 1
        and       LEFT(F_ANOMES, 4) in (@Max_Year, @Max_Year - 1)
    )
    

    and use the FULL JOIN but I think a better option is to simply group it by month:

    select
        [Month],
        SUM(CASE WHEN [Year] = @Max_Year - 1 THEN Monthly_Yield ELSE 0 END) Yield_Year_1,
        SUM(CASE WHEN [Year] = @Max_Year THEN Monthly_Yield ELSE 0 END) Yield_Year_0
    from Monthly_Yield_CTE
    group by [Month]
    order by [Month]
    

    One could use either the CTE version or re-write it without CTE as the query is simple enough:

    SET @ID_CARTERA = 8
    
    declare @Max_Year int
    select 
        @Max_Year = MAX(left(F_ANOMES, 4))
    from dr_rent_carteras_meses 
    where ID_CARTERA = @ID_CARTERA
    
    select  
        RIGHT(F_ANOMES, 2) Month,
        SUM(CASE 
                WHEN LEFT(F_ANOMES, 4) = @Max_Year - 1 
                THEN ROUND(POR_RENTABILIDAD, 2) 
                ELSE 0 
            END) Yield_Year_1,
        SUM(CASE 
                WHEN LEFT(F_ANOMES, 4) = @Max_Year 
                THEN ROUND(POR_RENTABILIDAD, 2) 
                ELSE 0 
            END) Yield_Year_0
    from      dr_rent_carteras_meses
    where     ID_CARTERA = @ID_CARTERA
    and       IND_RENTABILIDAD = 1
    and       LEFT(F_ANOMES, 4) in (@Max_Year, @Max_Year - 1)
    group by RIGHT(F_ANOMES, 2)
    order by 1
    

    The only drawback I’m aware of is that we get 0 instead of NULL for the months where the data is missing and I’m not sure if that’s important.

    Performance

    The performance of this query seems to be quite a bit better; in my setup the relative combined cost of the GROUP BY/CASE query with a separated MAX is about 30% vs 70% for FULL JOIN solution.

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

Sidebar

Related Questions

PROBLEM: Need the query to return the MONTH_NAME and YIELD for b.Monthly_Yield even when
Goal: Gain datatype date with year and month Problem: Need to help to convert
I need to query a table in an SQLite database to return all the
i have a problem with sql query. I need to get list of users.
To solve some problem I need to compute a variant of the pascal's triangle
i got some problem and need help .. my plan : 1. get ip
Having a bit of a problem getting my LINQ query to return the object
I have a complex problem to solve with SQL query. I need to generate
I need my LINQ Query to return the Product Datatype, after being grouped. It
I have a query that needs to return results that are NOT matched in

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.