TheChosenOne94
TheChosenOne94

Reputation: 103

convert rows to columns using date column

I have a table containing four rows: id(primary key, auto increment), value, type and time.

id   value  type    time
1    1.2     1    2017-10-26 16:16:49.350 
2    12.4    2    2017-10-26 16:16:49.350 
3    0.6     3    2017-10-26 16:16:49.350 
4    1.1     4    2017-10-26 16:16:49.350 
5    1.8     1    2017-10-25 14:12:24.650 
6    3.2     2    2017-10-25 14:12:24.650 
7    0.2     3    2017-10-25 14:12:24.650 
8    1.2     4    2017-10-25 14:12:24.650 

Is it possible to convert these rows to columns based on type and time(either by query or stored procedure)? something like this:

(type)1     2        3        4       time
1.2        12.4     0.6      1.1     2017-10-26 16:16:49.350
1.8        3.2      0.2      1.2     2017-10-25 14:12:24.650

PS: Each four types share the same time.

Upvotes: 3

Views: 2288

Answers (3)

Sean Lange
Sean Lange

Reputation: 33581

Here is another option using conditional aggregation or cross tab.

select Type1 = max(case when type = 1 then value)
    Type2 = max(case when type = 2 then value)
    Type3 = max(case when type = 3 then value)
    Type4 = max(case when type = 4 then value)
    , time
from YourTable
group by time

Upvotes: 3

Chris Mack
Chris Mack

Reputation: 5208

You can use PIVOT:

SELECT
    [1] type1
    , [2] type2
    , [3] type3
    , [4] type4
    , time
FROM 
    (
        SELECT
            value
            , type
            , time
        FROM table
    ) T
    PIVOT
    (
        SUM (value)
        FOR type IN
            (
                [1], [2], [3], [4]
            )
    ) P

Upvotes: 0

gotqn
gotqn

Reputation: 43636

Try this:

DECLARE @DataSource TABLE
(
    [id] SMALLINT
   ,[value] DECIMAL(9,1)
   ,[type] TINYINT
   ,[time] DATETIME2
);

INSERT INTO @DataSource ([id], [value], [type], [time])
VALUES (1, 1.2,  1, '2017-10-26 16:16:49.350')
      ,(2, 12.4, 2, '2017-10-26 16:16:49.350')
      ,(3, 0.6,  3, '2017-10-26 16:16:49.350')
      ,(4, 1.1,  4, '2017-10-26 16:16:49.350')
      ,(5, 1.8,  1, '2017-10-25 14:12:24.650')
      ,(6, 3.2,  2, '2017-10-25 14:12:24.650')
      ,(7, 0.2,  3, '2017-10-25 14:12:24.650')
      ,(8, 1.2,  4, '2017-10-25 14:12:24.650');

SELECT [1], [2], [3], [4], [time]
FROM
(
    SELECT [value], [type], [time]
    FROM @DataSource
) DS
PIVOT
(
    MAX([value]) FOR [type] IN ([1], [2], [3], [4])
) PVT
ORDER BY [time] DESC;

enter image description here

Upvotes: 2

Related Questions