Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PROBLEM WITH PIVOT TABLE

Status
Not open for further replies.

spiral123

Programmer
Sep 9, 2002
73
0
0
CA
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
DECLARE @PivotColumnHeaders VARCHAR(MAX)
SELECT TOP 4 @PivotColumnHeaders =
COALESCE(
@PivotColumnHeaders + ',[' + cast([TimeStamp] as varchar) + ']',
'[' + cast([TimeStamp] as varchar)+ ']'
)
FROM RecentTimestamps WHERE [SiteID] = @SiteID Order By [TimeStamp] Desc

DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'

SELECT *
FROM (SELECT [Header], [Value], [TimeHeader] FROM TimeDataGather Where [SiteID] = ' + @SiteID + ' ) as s PIVOT (Max([Value]) FOR [TimeHeader] IN (' + @PivotColumnHeaders + ')) AS p
Order By [Header] Desc'

EXECUTE(@PivotTableSQL)
END







 
found my issue, I used cast([TimeStamp] as varchar) in the headers but my [Timeheader] field was still datetime thanks though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top