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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:23:25+00:00 2026-05-27T08:23:25+00:00

Running this query in sql server 2008 now, but soon plan to move it

  • 0

Running this query in sql server 2008 now, but soon plan to move it in creating a report in sql reporting services:

SELECT * from ( SELECT Amount, Year, column1, column2,column3 from BUYSCTE ) BUY 

My table results running above query without the pivot is this

Column1 | Column2 | Column3| FYYear| Amount|

 1           cat     dog      2011   50 
 1           cat     dog      2012   75
 1           cat     dog      2013   65
 2          fish     snake    2011   23
 2          fish     snake    2012   39
 2          fish     snake    2013   59
 .. 
 ..  
 ..                           2016

so basically I want the results to end up like this:

Column1 | Column2 | Column3| 2011| 2012 | 2013 
 1           cat     dog      50    75     65
 2          fish     snake    23    39     59

so the query I came up with to make this happen is I first created a table variable and made it a dynamic variable and did a pivot like this,

declare @Year nvarchar(Max)
set @Year = STUFF(
(SELECT ', ' + quotename(FYYear)
from BUYSCTE Group By FYYear order by
FYYear For XML PATH(''))
, 1, 2, '');

The above populates the @Year value with: [2011], [2012], [2013], [2014], [2015], [2016]

I know that because I change the above select to SELECT @Year just to check if the value is set correctly

from there I change the above select after the table variable to

SELECT * from ( SELECT Amount, FYYear, column1, column2,column3 from BUYSCTE ) BUY 
PIVOT( SUM(Amount) FOR FYYear in ([@Year]) ) pvt

but my results end up being this

 Column1 | Column2 | Column3| @Year|
 1           cat     dog      null   
 2          fish     snake    null

What am I doing wrong? it seems like something small I am missing that I cannot see myself. even if I change the aggregate function from SUM to COUNT it gives 0 for the @Year column values instead of null

  • 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-27T08:23:26+00:00Added an answer on May 27, 2026 at 8:23 am

    You are close, but for this to work you have to construct your PIVOT using dynamic SQL and then execute it. So, after you populate your variable @Year, you need to do something like this:

    DECLARE @Query VARCHAR(MAX)
    
    SET @Query = '
    SELECT * from ( SELECT Amount, FYYear, column1, column2,column3 from BUYSCTE ) BUY 
    PIVOT( SUM(Amount) FOR FYYear in ('+ @Year + ') ) pvt'
    
    EXEC(@Query)
    

    Though before doing this, you should take a look at this link.

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

Sidebar

Related Questions

I've got 2 servers running SQL Server 2008, and I have the following query:
I'm running SQL Server 2008. I've built a large search query (contained in a
On Sql Server 2008, I have a slow-running update query that has been working
I am running an SQL query on a SQL Server 2008 database. The results
I have a SQL Query running on SQL Server 2008 R2 that returns a
Im running this query on the same server as the web application, so SPQuery.ExpandRecurrence
I have a SQL Server 2008 database and I have a problem with this
In this live SQL Server 2008 (build 10.0.1600) database, there's an Events table, which
I need some help in explaining this behavior in SQL Server 2008. I have
I have a website running on a Windows 2008 R2 server, using a SQL

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.