dbTalk Databases Forums  

Pivot table from Historian Data

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Pivot table from Historian Data in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mcolson
 
Posts: n/a

Default Pivot table from Historian Data - 01-07-2011 , 01:08 PM






I have data that has been captured in historian. I'm trying to create
a view that displays data captured via a Historian in a Pivot Table.
I'm having trouble with using the PIVOT operator. Any help on how
this could be turned into a pivot table?

SELECT DateTime, TagName, Value AS Pressure
FROM History
WHERE (DateTime >= CONVERT(DATETIME, '2010-12-22 00:00:00', 102))
AND (TagName IN ('Rough', 'HiVac1', 'Leg1', 'Leg2')

'What I get
2010/12/23 Rough 2.100
2010/12/23 HiVac1 1433.59
2010/12/23 Leg1 1536
2010/12/23 Leg2 0.40
2010/12/24 Rough 2.100
2010/12/24 HiVac1 3276.80
2010/12/24 Leg1 3379
2010/12/24 Leg2 2.10

'What I'm trying to get
Rough HiVac1 Leg1 Leg2
2010/12/23 2.100 1433.59 1536 0.40
2010/12/24 2.100 3276.80 3379 2.10

Thanks in advance for any help.

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Pivot table from Historian Data - 01-07-2011 , 01:59 PM






mcolson wrote:
Quote:
I have data that has been captured in historian. I'm trying to create
a view that displays data captured via a Historian in a Pivot Table.
I'm having trouble with using the PIVOT operator. Any help on how
this could be turned into a pivot table?

SELECT DateTime, TagName, Value AS Pressure
FROM History
WHERE (DateTime >= CONVERT(DATETIME, '2010-12-22 00:00:00', 102))
AND (TagName IN ('Rough', 'HiVac1', 'Leg1', 'Leg2')

'What I get
2010/12/23 Rough 2.100
2010/12/23 HiVac1 1433.59
2010/12/23 Leg1 1536
2010/12/23 Leg2 0.40
2010/12/24 Rough 2.100
2010/12/24 HiVac1 3276.80
2010/12/24 Leg1 3379
2010/12/24 Leg2 2.10

'What I'm trying to get
Rough HiVac1 Leg1 Leg2
2010/12/23 2.100 1433.59 1536 0.40
2010/12/24 2.100 3276.80 3379 2.10

Thanks in advance for any help.
What version of SQL Server? If pre-2005, google for SQL pivot. If 2005+.
look for the pivot topics in SQL BOL (Books Online)

Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Pivot table from Historian Data - 01-07-2011 , 04:43 PM



mcolson (mcolson1590 (AT) gmail (DOT) com) writes:
Quote:
I have data that has been captured in historian. I'm trying to create
a view that displays data captured via a Historian in a Pivot Table.
I'm having trouble with using the PIVOT operator. Any help on how
this could be turned into a pivot table?

SELECT DateTime, TagName, Value AS Pressure
FROM History
WHERE (DateTime >= CONVERT(DATETIME, '2010-12-22 00:00:00', 102))
AND (TagName IN ('Rough', 'HiVac1', 'Leg1', 'Leg2')
...
'What I'm trying to get
Rough HiVac1 Leg1 Leg2
2010/12/23 2.100 1433.59 1536 0.40
2010/12/24 2.100 3276.80 3379 2.10
SELECT DateTime,
MAX(CASE TagName WHEN 'Rough' THEN Value END) AS Rough,
MAX(CASE TagName WHEN 'HiVac1' THEN Value END) AS HiVac1,
MAX(CASE TagName WHEN 'Leg1' THEN Value END) AS Leg1,
MAX(CASE TagName WHEN 'Leg2' THEN Value END) AS Leg2
FROM History
WHERE DateTime >= '20101222'
GROUP BY DateTime

You may note that I don't actually use the PIVOT operator. I think
the above works just as well, and it runs about any DBMS. I have not
even bothered to learn the PIVOT syntax.

Note also the comparison on the date value. The format YYYYMMDD is always
interpreted the same, so this is a safe format.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.