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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:24:13+00:00 2026-05-11T21:24:13+00:00

I have two tables (1) MonthlyTarget {SalesManCode, TargetMonthYear, TargetValue}; this table has 1966177 rows.

  • 0

I have two tables

(1) MonthlyTarget {SalesManCode, TargetMonthYear, TargetValue}; this table has 1966177 rows.

(2) MonthlySales  {SalesManCode, SaleDate, AchievedValue};

this table has 400310 rows.

I have to make a query that produces a result like the following table:

{SalesManCode, JanTar, JanAch, FebTar, FebAch,....., DecTar, DecAch}

The problem is, joining these two tables taking a long time.

What should be the query?

How can the query be optimized?

I don’t want to consider indexing.

  • 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-11T21:24:13+00:00Added an answer on May 11, 2026 at 9:24 pm

    It looks like you’re missing some columns in your MonthlyTarget table, namely a “TargetDate” column.

    In addition to what everyone has already said about indexing, sometimes a divide-and-conquer approach can really help. Rather than joining a 1966177 row table to a 400310 row table, create to tiny temp tables and join them together instead:

    CREATE TABLE #MonthlySalesAgg
    (
        SalesManCode int,
        JanTar money,
        FebTar money,
        MarTar money,
        AprTar money,
        MayTar money,
        JunTar money,
        JulTar money,
        AugTar money,
        SepTar money,
        OctTar money,
        NovTar money,
        DecTar money
    
        PRIMARY KEY CLUSTERED (SalesManCode)
    )
    
    INSERT INTO #MonthlySalesAgg
    SELECT *
    FROM
    (SELECT SalesManCode, TargetValue, SaleMonth = Month(TargetDate) FROM MonthlyTarget) as temp
    PIVOT
    (
        Max(TargetValue)
        FOR [SaleMonth] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
    ) as p
    
    CREATE TABLE #MonthlyTargetAgg
    (
        SalesManCode int,
        JanAch money,
        FebAch money,
        MarAch money,
        AprAch money,
        MayAch money,
        JunAch money,
        JulAch money,
        AugAch money,
        SepAch money,
        OctAch money,
        NovAch money,
        DecAch money
    
        PRIMARY KEY CLUSTERED (SalesManCode)
    )
    
    INSERT INTO #MonthlyTargetAgg
    SELECT * FROM
    (SELECT SalesManCode, AchievedValue, SaleMonth = Month(SaleDate) FROM MonthlySales) as temp
    PIVOT
    (
        Sum(AchievedValue)
        FOR [SaleMonth] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
    ) as p
    

    The queries above create two intermediate tables which should contain the same number of records as your SalesMan table. Joining them is straightforward:

    SELECT *
    FROM #MonthlyTargetAgg target
    INNER JOIN #MonthlySalesAgg sales ON target.SalesManCode = sales.SalesManCode
    

    If you find yourself needing to pull out data by month all the time, move the code into a view instead.

    PIVOT requires SQL Server 2005 or higher, and its often a very useful operator. Hopefully SQL Server 2008 will allow users to pivot on more than one column at a time, which will result in an even simpler query than shown above.

    Using SQL Server 2000:

    PIVOT is syntax sugar. For example,

    SELECT * FROM
    (SELECT SalesManCode, AchievedValue, SaleMonth = Month(SaleDate) FROM MonthlySales) as temp
    PIVOT
    (
        Sum(AchievedValue)
        FOR [SaleMonth] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
    ) as p
    

    Becomes

    SELECT
        SalesManCode,
        [1] = Sum(case SaleMonth when 1 then AchievedValue else 0 end),
        [2] = Sum(case SaleMonth when 2 then AchievedValue else 0 end),
        [3] = Sum(case SaleMonth when 3 then AchievedValue else 0 end),
        [4] = Sum(case SaleMonth when 4 then AchievedValue else 0 end),
        [5] = Sum(case SaleMonth when 5 then AchievedValue else 0 end),
        [6] = Sum(case SaleMonth when 6 then AchievedValue else 0 end),
        [7] = Sum(case SaleMonth when 7 then AchievedValue else 0 end),
        [8] = Sum(case SaleMonth when 8 then AchievedValue else 0 end),
        [9] = Sum(case SaleMonth when 9 then AchievedValue else 0 end),
        [10] = Sum(case SaleMonth when 10 then AchievedValue else 0 end),
        [11] = Sum(case SaleMonth when 11 then AchievedValue else 0 end),
        [12] = Sum(case SaleMonth when 12 then AchievedValue else 0 end)
    FROM
        (SELECT SalesManCode, AchievedValue, SaleMonth = Month(SaleDate) FROM MonthlySales) as temp
    GROUP BY SalesManCode
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables (1) MonthlyTarget {SalesManCode, TargetValue}; (2) MonthlySales {SalesManCode, SaleDate, AchievedValue}; I
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables, orders and old_orders. Each table has some similar fields and
I have two tables and want to compare rows on sqlite like this table1
I have two tables that are joined together. A has many B Normally you
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I have two tables: users and user_depts. Let's say (for this question) that users
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay
I have two tables CREATE TABLE table1 (id int primary key auto_increment,....); CREATE TABLE
I have two tables. One table contains information about Assets, another Table about their

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.