Solved

SQL Syntax

Posted on 2012-04-13
5
280 Views
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.



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
0
Comment
Question by:MoreThanDoubled
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Author Comment

by:MoreThanDoubled
ID: 37844509
Keep in mind that data will be added every year.
0
 
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.
0
 

Author Comment

by:MoreThanDoubled
ID: 37846515
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
 
LVL 41

Accepted Solution

by:
ralmada earned 500 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 (
            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
 

Author Comment

by:MoreThanDoubled
ID: 37848674
ralmada,

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


Thank you soooo much!!!
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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