?
Solved

SQL Syntax

Posted on 2012-04-13
5
Medium Priority
?
283 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 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 (
            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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

762 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