Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic Column Header


Dynamic Column Header

Author
Message
prakashdotc
prakashdotc
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 66
Hi All,

I already have a query which retrieve data based on various dates, now i need is the Column header with those 'Date'.

Dynamic Dates as Column Header.

Is there any way to do this SQL Server 2005.

Thanks in Advance.

Prakash.C
RBarryYoung
RBarryYoung
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10034 Visits: 9517
prakashdotc (12/21/2008)
Is there any way to do this SQL Server 2005.

Yes.

If you could supply us with some details or examples of what you want, we could demonstrate it for you.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
krayknot
krayknot
Old Hand
Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)

Group: General Forum Members
Points: 382 Visits: 534
prakashdotc (12/21/2008)
Hi All,

I already have a query which retrieve data based on various dates, now i need is the Column header with those 'Date'.

Dynamic Dates as Column Header.

Is there any way to do this SQL Server 2005.

Thanks in Advance.

Prakash.C



Thsi is a sample only

declare @colname varchar(100)
declare @rowcolname varchar(1000)
set @rowcolname = ''

declare col CURSOR FOR
select id from actionstate

open col
fetch next from col into @colname
while @@fetch_status = 0
begin
set @rowcolname = @rowcolname + '[' + @colname + '] int,'
fetch next from col into @colname
end
close col
deallocate col

print('Create TAble tablename('+ @rowcolname + ')')
exec('Create TAble tablename('+ @rowcolname + ')')

select * from tablename
drop tablename

kshitij kumar
kshitij@krayknot.com
www.krayknot.com
prakashdotc
prakashdotc
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 66
Hi All,

Here's my description about my Query

i Have a Table with Columns as ProdId, Value, Date
Users will enter some value for each product on various date

The Task is to Retrieve the last 5 days record, with 5 Date's as it's header like

--------------------------------------------------------------------------------------------
ProductId - Date 1 - Date 2 - Date 3 - Date 4 - Date 5
--------------------------------------------------------------------------------------------
1 - 234 - 989 - 876 - 7686 - 9878
2 - 34 - 99 - 876 - 66 - 87
--------------------------------------------------------------------------------------------

Thanks

Prakash.C
Kishore.P
Kishore.P
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1431 Visits: 619
prakashdotc (12/22/2008)
Hi All,

Here's my description about my Query

i Have a Table with Columns as ProdId, Value, Date
Users will enter some value for each product on various date

The Task is to Retrieve the last 5 days record, with 5 Date's as it's header like

--------------------------------------------------------------------------------------------
ProductId - Date 1 - Date 2 - Date 3 - Date 4 - Date 5
--------------------------------------------------------------------------------------------
1 - 234 - 989 - 876 - 7686 - 9878
2 - 34 - 99 - 876 - 66 - 87
--------------------------------------------------------------------------------------------

Thanks

Prakash.C


In SQL 2005, use PIVOT function you get expected result, for more info see SQL BOL.



prakashdotc
prakashdotc
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 66
The Problem is not retreving records in this format, but the Column Header must be Dynamic Date

--------------------------------------------------------------------------------------------
ProductId - Date 1 - Date 2 - Date 3 - Date 4 - Date 5
--------------------------------------------------------------------------------------------
1 - 234 - 989 - 876 - 7686 - 9878
2 - 34 - 99 - 876 - 66 - 87
--------------------------------------------------------------------------------------------

like this

--------------------------------------------------------------------------------------------
ProductId - 12-12-2008 - 13-12-2008 - 14-12-2008 - 15-12-2008 - 16-12-2008
--------------------------------------------------------------------------------------------
1 - 234 - 989 - 876 - 7686 - 9878
2 - 34 - 99 - 876 - 66 - 87
--------------------------------------------------------------------------------------------
nitinpatel31
nitinpatel31
SSC Eights!
SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)SSC Eights! (889 reputation)

Group: General Forum Members
Points: 889 Visits: 293
Hi,

You can create dynamic query for PIVOT and then run it using sp_executesql.

Best Regards
Nitin

http://www.enlinkURL.com

Regards,
Nitin
Kishore.P
Kishore.P
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1431 Visits: 619
prakashdotc (12/22/2008)
The Problem is not retreving records in this format, but the Column Header must be Dynamic Date

--------------------------------------------------------------------------------------------
ProductId - Date 1 - Date 2 - Date 3 - Date 4 - Date 5
--------------------------------------------------------------------------------------------
1 - 234 - 989 - 876 - 7686 - 9878
2 - 34 - 99 - 876 - 66 - 87
--------------------------------------------------------------------------------------------

like this

--------------------------------------------------------------------------------------------
ProductId - 12-12-2008 - 13-12-2008 - 14-12-2008 - 15-12-2008 - 16-12-2008
--------------------------------------------------------------------------------------------
1 - 234 - 989 - 876 - 7686 - 9878
2 - 34 - 99 - 876 - 66 - 87
--------------------------------------------------------------------------------------------


Try like the following code:


use AdventureWorks
go

declare @FromDate datetime
, @Todate datetime
, @COLName varchar (max)
, @SQL varchar (max)

select @FromDate = '2002-12-01'
, @ToDate = '2004-01-30'
, @COLName = ''
, @SQL = ''

while @FromDate <= @ToDate
begin
if (@COLName = '')
begin
set @COLName = '[' + convert (varchar (10), @FromDate, 121) + ']'
end
else
begin
set @COLName = @COLName + ', [' + convert (varchar (10), @FromDate, 121) + ']'
end
set @FromDate = dateadd (d, 1, @FromDate)
end
print @COLName
select @SQL = 'select VendorID, ' + @COLName +
' From (select PurchaseOrderID, OrderDate, VendorID from Purchasing.PurchaseOrderHeader) as P
pivot
( count (PurchaseOrderID)
for OrderDate in (' + @COLName + ')
) as pvt
order by VendorID
'
print @SQL
exec (@SQL)
go


Note: This work on SQL 2005 and above versions.



BI_NewBie
BI_NewBie
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 157
Hi,

I have similar issue.

I have managed to create pivot in SQL 2005 for date column header but problem is while exporting the data into some flat file i am not able to retrieve Column header. infact i cant map them since the column are dynamic.

Kindly help me in this regard,


Deepika
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search