SQL Syntax

Posted on 2012-04-13
Medium Priority
Last Modified: 2012-08-14
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.

  FROM table1 t1
  inner join table2 t2 on t1.DataId = t2.DataID

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

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

Question by:MoreThanDoubled
  • 3

Author Comment

ID: 37844509
Keep in mind that data will be added every year.
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 37846173
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.

Author Comment

ID: 37846515

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?
LVL 41

Accepted Solution

ralmada earned 2000 total points
ID: 37847406
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 (
              FROM table1 t1
              inner join table2 t2 on t1.DataId = t2.DataID) o
            pivot (MAx(Amount) for [Date] in (' + @cols + ')) as p'

Author Comment

ID: 37848674

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

Thank you soooo much!!!

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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.

Join & Write a Comment

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question