sai bharath
sai bharath

Reputation: 59

How to get this kind of data?

I have data some thing like this:

+---------+---------+---------+-------+
| MAXIMUM | MINIMUM | SENSORS | TIME  |
+---------+---------+---------+-------+
|      10 |      12 |      14 | 13:12 |
|      80 |      70 |     100 | 14:54 |
+---------+---------+---------+-------+

But I need something like this:

+---------+-------+
|    X    |   Y   |
+---------+-------+
| MAXIMUM | 10    |
| MINIMUM | 12    |
| SENSORS | 14    |
| TIME    | 13:12 |
| MAXIMUM | 80    |
| MINIMUM | 70    |
| SENSORS | 100   |
| TIME    | 14:54 |
+---------+-------+

How to get this kind of data is there any possibility to get data?

Upvotes: 2

Views: 53

Answers (2)

John Cappelletti
John Cappelletti

Reputation: 81930

Just another option

Example

Select B.*
 From  YourTable
 Cross Apply (values ('MAXIMUM',convert(nvarchar(50),MAXIMUM))
                    ,('MINIMUM',convert(nvarchar(50),MINIMUM))
                    ,('SENSORS',SENSORS)
                    ,('TIME'   ,convert(nvarchar(50),[TIME],108))
             ) B(x,y)

Returns

x          y
MAXIMUM    10
MINIMUM    12
SENSORS    14
TIME       13:12:00
MAXIMUM    80
MINIMUM    70
SENSORS    100
TIME       14:54:00

Upvotes: 3

Andrea
Andrea

Reputation: 12355

You can use UNPIVOT:

declare @tmp table (MAXIMUM  nvarchar(10), MINIMUM  nvarchar(10), SENSORS   nvarchar(10), [TIME] nvarchar(10))
insert into @tmp select    10,12,14 ,'13:12'
insert into @tmp select    80,70,100,'14:54'

select u.x,u.y
from @tmp s 
unpivot 
( 
[y] 
for [x] in ([MAXIMUM],[MINIMUM],[SENSORS],[TIME]) 
) u;

Results:

enter image description here

Upvotes: 2

Related Questions