Solved

SQL Syntax

Posted on 2012-04-13
5
268 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
  • 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:yodercm
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now