Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a logger table with timestamp, tag name and value rows that logs data each 5 seconds automatically for several tags.
I want to run query that shows data collected in pivot display - timestamp, value for tag1, value for tag2,... value for tag n...
Can please someone recommend me a way to build this query for displaying data?

What I have tried:

I tried to look for help on google and only found way to run PIVOT query with SUM/AVG of values, but not presenting values by timestamp.
Posted
Updated 22-Dec-17 8:11am
Comments
ZurdoDev 19-Dec-17 14:00pm    
It's hard to know how to help without seeing how your data is stored and how you want it presented. Click Improve question and add that information please.

Without knowing your data structure I am guessing here, however, I've created my own below
CREATE TABLE [dbo].[Test](
	[dtime] [datetime] NULL,
	[tag] [int] NULL,
	[value] [int] NULL
) ON [PRIMARY]


Your logger is storing data every 5 seconds so the records will be unique for a given datetime and hence you can use the avg or sum and it will still return the actual value so the pivot will look something like

select * from test pivot (sum(value) for tag in ([1],[2],[3])) as tagvalue where dtime > '2017-12-20'


My test data is something like

dtime	tag	value
2017-12-20 11:30:00.000	1	2
2017-12-20 11:30:00.000	2	5
2017-12-20 11:30:00.000	3	8


and this will give a result of
2017-12-20 11:30:00.000	2	5	8


You mention a timestamp, Im assuming it is a datetime field not a timestamp field as timestamp are unique for each record and you will not be able to pivot it.
 
Share this answer
 
v3
Here is an example on how to cast a column then pivot

SQL
DECLARE  @DATA_LOG TABLE(
	[timestamp] Datetime NULL,
	[point_id] VARCHAR(10) NULL,
	[_val] VARCHAR(10) NULL
)

INSERT INTO @DATA_LOG
	SELECT GETDATE()-1,	1, 2 UNION
	SELECT GETDATE()-1,	2, 5 UNION
	SELECT GETDATE()-1,	3, 8 

select * from (select [timestamp], [point_id],cast([_val] as int) as [_val] from @DATA_LOG) p 
 pivot(sum([_val]) for point_id in ([1],[2],[3])) as tagvalue where [timestamp] >= DATEADD(day,-4,GetDate())


Output:
timestamp	            1	2	3
2017-12-18 23:44:29.703	2	5	8


Reference: PIVOT Query Conversion For Varchar(40) to int or Decimal[^]
 
Share this answer
 
v2
Comments
Amit Golan 20-Dec-17 2:50am    
Thank you Bryian for your help. It works just fine!!!
Bryian Tan 20-Dec-17 3:11am    
You're welcome.
Thanks for the lead.

My existing table's name is DATA_LOG and it has timestamp field with datetime data type, point_id field with varchar data type and _val field with varchar data type.
I need to cast the _val field to float data type, so I tried the following query and it doesn't work:
select * FROM DATA_LOG 
pivot(sum(cast(_VAL as float)) for point_id in ([1],[2],[3])) as tagvalue where timestamp >= DATEADD(day,-4,GetDate())

I can't cast the data type inside the pivot. I also tried to cast it in the select part and it still doesn't work.
where should I place the cast of the data type?
 
Share this answer
 
Comments
Richard Deeming 20-Dec-17 12:02pm    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your reply as a new "solution".
Hi,
Follow the below links.

</p?

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900