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 derfloh 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
Joined
Sep 9, 2002
Messages
73
Location
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







 
What exactly is the problem?

Instead of Execute (@PivotTableSQL)

do

print @PivotTableSQL

and then try running it

It would be much easier to spot the problem.

PluralSight Learning Library
 
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