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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:35:18+00:00 2026-06-12T12:35:18+00:00

I’m sure this won’t be a difficult question for the hardcore SQL geeks, but

  • 0

I’m sure this won’t be a difficult question for the hardcore SQL geeks, but I need some help. This is for SQL Server 2000 (inherited projects!).

I have a table of salary listings that look like this:

EmployeeID  |  EffectiveDate  | Salary
1           |   2/1/2011      | 500
1           |   6/1/2011      | 600
1           |   12/1/2011     | 650

I need to create a query that will output these salaries by month for a given year. So the output would be like

EmployeeID  | Jan | Feb | Mar | Apr | Apr | May | Jun | Jul | Aug | Sept | Oct | Nov | Dec
1           | 500 | 500 | 500 | 500 | 500 | 500 | 600 | 600 | 600 | 600  | 600 | 600 | 650

I know there must be a way to do this effectively with SQL, I just can’t seem to get it right. Obviously, I would be naming the month columns above with SQL such as
SELECT EmployeeID, ‘Jan’ AS Jan, ‘Feb’ AS Feb, etc but the rest of the statement is harder since I’m looking for ranges.

  • 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-12T12:35:19+00:00Added an answer on June 12, 2026 at 12:35 pm

    Since you are using SQL Server 2000 it does not have a PIVOT function, so you will have to replicate this using an aggregate function and a CASE statement. Similar to this:

    select  employeeid,
      sum(case when DatePart(Month, EffectiveDate) = 1 then Salary end) as Jan,
      sum(case when DatePart(Month, EffectiveDate) = 2 then Salary end) as Feb,
      sum(case when DatePart(Month, EffectiveDate) = 3 then Salary end) as Mar,
      sum(case when DatePart(Month, EffectiveDate) = 4 then Salary end) as Apr,
      sum(case when DatePart(Month, EffectiveDate) = 5 then Salary end) as May,
      sum(case when DatePart(Month, EffectiveDate) = 6 then Salary end) as Jun,
      sum(case when DatePart(Month, EffectiveDate) = 7 then Salary end) as Jul,
      sum(case when DatePart(Month, EffectiveDate) = 8 then Salary end) as Aug,
      sum(case when DatePart(Month, EffectiveDate) = 9 then Salary end) as Sep,
      sum(case when DatePart(Month, EffectiveDate) = 10 then Salary end) as Oct,
      sum(case when DatePart(Month, EffectiveDate) = 11 then Salary end) as Nov,
      sum(case when DatePart(Month, EffectiveDate) = 12 then Salary end) as Dec
    from  yourtable
    group by employeeid
    

    See SQL Fiddle with Demo

    Edit, based on your comments above carrying over the value from one month to the next, here is a solution that might work for you.

    declare @query as nvarchar(max) = '',
      @rowcount as int = 1,
      @pivotrow as int,
      @currentMonthSalary as int = 0,
      @priorMonthSalary as int = 0,
      @employeeid int 
    
    select distinct effectivedate
    into #colspivot
    from yourtable
    
    while @rowcount <= 12 -- loop thru each month
      begin
    
        set @pivotrow = (select top 1 datepart(month, effectivedate)
                            from #colspivot
                            order by datepart(month, effectivedate))
    
        select @currentMonthSalary = salary, @employeeid = EmployeeID
                from yourtable
                where datepart(month, effectivedate) = @pivotrow
    
        if @pivotrow = @rowcount
            begin
                insert into FinalData (employeeid, effectivemonth, salary)
                select @employeeid, cast(DateName(month, DateAdd(month, @pivotrow, 0) -1) as varchar(3)), @currentMonthSalary
    
                set @query = @query + ', sum(case when effectivemonth = ''' +  cast(DateName(month, DateAdd(month, @pivotrow, 0) -1) as varchar(3)) + ''' 
                                            then ' + cast(@currentMonthSalary as varchar(10)) + ' end) as '+ cast(DateName(month, DateAdd(month, @pivotrow, 0) -1) as varchar(3))
    
                delete from #colsPivot where datepart(month, effectivedate) = @pivotRow
    
                set @priorMonthSalary = @currentMonthSalary
            end
        else
            begin
                insert into FinalData (employeeid, effectivemonth, salary)
                select @employeeid, cast(DateName(month, DateAdd(month, @rowcount, 0) -1) as varchar(3)), @priorMonthSalary
    
                set @query = @query + ', sum(case when effectivemonth = ''' + cast(DateName(month, DateAdd(month, @rowcount, 0) -1) as varchar(3)) + '''  
                            then ' +  cast(@priorMonthSalary as varchar(10)) + ' end) as '+cast(DateName(month, DateAdd(month, @rowcount, 0) -1) as varchar(3))
            end
    
        if @rowcount <= 12
          set @rowcount = @rowcount + 1
      end
    
    set @query = 'select employeeid '+ @query 
                  + ' from FinalData group by employeeid;'
    
    exec(@query) 
    

    See SQL Fiddle with Demo. I created a new table FinalData to store the data for each month while I loop through creating the sql statement.

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

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.