SQL Syntax

Hi All,

I asked a very similar question in a previous post, but have another question regarding pivot data.
This should be fairly simple.  
i am using a Telerik Grid and sql server 2008 R2.

what would be the best way to display the data below pivoted by Date in a Grid.



QUERY
SELECT
      DataId
      ,JunkID
      ,Amount
      ,Date
  FROM table1 t1
  inner join table2 t2 on t1.DataId = t2.DataID


DATA DISPLAYED
DataId   JunkId   Amount      Date
Data1      Junk      100.36      201101
Data1      Junk      101.36      201102
Data1      Junk      102.36      201103
Data1      Junk      103.36      201104
Data1      Junk      104.36      201105
Data1      Junk      105.36      201106
Data1      Junk      106.36      201107
Data1      Junk      107.36      201108
Data1      Junk      108.36      201109
Data1      Junk      109.36      201110
Data1      Junk      110.36      201111
Data1      Junk      111.36      201112
Data1      Junk      112.36      201201
Data1      Junk      113.36      201202
Data1      Junk      114.36      201203
Data1      Junk      115.36      201204
Data1      Junk      116.36      201205
Data1      Junk      117.36      201206
Data1      Junk      118.36      201207
Data1      Junk      119.36      201208
Data1      Junk      120.36      201209
Data1      Junk      121.36      201210
Data1      Junk      122.36      201211
Data1      Junk      123.36      201212


PIVOT Data
DateID      JunkID      201101      201102      201103      201104      201105      201106      201107      201108      201109      201110      201111      201112      201101      201102      201103      201104      201105      201106      201107      201108      201109      201110      201111      201112
Data1      Junk      100.36      101.36      102.36      103.36      104.36      105.36      106.36      107.36      108.36      109.36      110.36      111.36      112.36      113.36      114.36      115.36      116.36      117.36      118.36      119.36      120.36      121.36      122.36      123.36

I have attached a txt document for a clearer view of how the data should be displayed in the grid.

I am having no luck with getting this to work right so your help will be greatly appreciated.
I am desparate for the solution :)


Thanks
MoreThanDoubledAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MoreThanDoubledAuthor Commented:
Keep in mind that data will be added every year.
0
Cornelia YoderArtistCommented:
How much data is there?  The obvious solution would be to put the data into a php array and then sort/manipulate it as you need, then display it.
0
MoreThanDoubledAuthor Commented:
yodercm,

There will be data entered into the database monthly but i will only need to pull the current year and the forcasted next year.

is a php array a .net thing?

Does anyone know of a possible way to do this in ASP.NET using possible a datatable or something?
0
ralmadaCommented:
try the following:

declare @strSQL varchar(max)
declare @cols varchar(max)

set @cols = stuff((select distinct  '], [' + cast([date] as varchar(max))
                  FROM table1 t1  inner join table2 t2 on t1.DataId = t2.DataID order by 1 for xml path('')), 1, 2, '') + ']'

set @strSQL = 'select DataID, JunkID, ' + @cols + ' from (
            SELECT
                  DataId
                  ,JunkID
                  ,Amount
                  ,[Date]
              FROM table1 t1
              inner join table2 t2 on t1.DataId = t2.DataID) o
            pivot (MAx(Amount) for [Date] in (' + @cols + ')) as p'
exec(@strSQL)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MoreThanDoubledAuthor Commented:
ralmada,

You are a true GENIUS, it worked!!!
Thanks for the knowledge on the STUFF function.


Thank you soooo much!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.