snippetsqlMinor
How to transpose rows into columns in SQL?
Viewed 0 times
rowscolumnstransposeintosqlhow
Problem
Sorry for misleading question but I don't know hot to name my issue.
My db<>fiidle is here.
I have a table with defined as follows:
It contains the following data:
And as a result I would like to get:
How can I achieve that?
My db<>fiidle is here.
I have a table with defined as follows:
CREATE TABLE shift (
worker_name VARCHAR(25),
log_time DATETIME,
in_out VARCHAR(3)
);It contains the following data:
INSERT INTO shift VALUES ('User 1', '2018-05-14 07:24:40.000', 'IN');
INSERT INTO shift VALUES ('User 2', '2018-05-14 11:55:29.000', 'IN');
INSERT INTO shift VALUES ('User 3', '2018-05-14 14:03:26.000', 'IN');
INSERT INTO shift VALUES ('User 1', '2018-05-14 14:05:08.000', 'Out');
INSERT INTO shift VALUES ('User 3', '2018-05-14 20:08:30.000', 'Out');
INSERT INTO shift VALUES ('User 2', '2018-05-14 20:08:36.000', 'Out');
INSERT INTO shift VALUES ('User 4', '2018-05-14 20:09:14.000', 'Out');And as a result I would like to get:
worker_name | IN | OUT
:---------- | :------------------ | :------------------
User 1 | 14/05/2018 07:24:40 | 14/05/2018 14:05:08
User 2 | 14/05/2018 11:55:29 | 14/05/2018 20:08:36
User 3 | 14/05/2018 14:03:26 | 14/05/2018 20:08:30
User 4 | ---------- | 14/05/2018 20:09:14How can I achieve that?
Solution
This is an example of pivoting:
The idea is to map
In general, the presentation of data is better handled by the application layer. So I would suggest reading the table as is, and let the presentation of the data be handled by the application.
select worker_name, max(_in) as _in, max(_out) as _out
from (
select worker_name
, case when in_out = 'IN' then log_time end as _in
, case when in_out = 'OUT' then log_time end as _out
from shift
) as t
group by worker_name;The idea is to map
log_time to _in when in_out = 'IN', ditto for _out. In the outer select an aggregate function is used to filter out nulls. Try running the inner select first to get an idea of what is happening.In general, the presentation of data is better handled by the application layer. So I would suggest reading the table as is, and let the presentation of the data be handled by the application.
Code Snippets
select worker_name, max(_in) as _in, max(_out) as _out
from (
select worker_name
, case when in_out = 'IN' then log_time end as _in
, case when in_out = 'OUT' then log_time end as _out
from shift
) as t
group by worker_name;Context
StackExchange Database Administrators Q#207197, answer score: 7
Revisions (0)
No revisions yet.