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
-- 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