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 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

Ask A Question

Stats

  • Questions 154k
  • Answers 154k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can try to push to an array and then… May 12, 2026 at 10:30 am
  • Editorial Team
    Editorial Team added an answer You need to add the /clr compiler flag only to… May 12, 2026 at 10:30 am
  • Editorial Team
    Editorial Team added an answer And selector is the answer here as well. Search for… May 12, 2026 at 10:29 am

Related Questions

I have two tables (1) MonthlyTarget {SalesManCode, TargetValue}; (2) MonthlySales {SalesManCode, SaleDate, AchievedValue}; I
I am using MVC RC2. I have Two tables 1)Product (PID, PName, CIDfk); 2)Category(CID,
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I have two tables with a 1:n relationship: "content" and "versioned-content-data" (for example, an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.