I have a table as below, i want to get the OUT column value below the IN column and adding a new record.
create table Temp (
[name] varchar(10),
[In] varchar(10),
[Out] varchar(10),
[HRs] varchar(10),
);
insert into Temp values('bob','login','logout','8');
insert into Temp values('alice','login','logout','9');
insert into Temp values('peter','login',null,'0');
Table output
NAME | IN | OUT | HRs |
---|---|---|---|
bob | login | logout | 8 |
alice | login | logout | 9 |
peter | login | NULL | 0 |
Expected Output
NAME | IN | HRs |
---|---|---|
bob | login | 8 |
bob | logout | 8 |
alice | login | 9 |
alice | logout | 9 |
peter | login | 0 |
peter | NULL | 0 |
I need to achieve the above output on the Temp table combining the IN & OUT columns into IN column, how to achieve this in DB2 sql
One approach uses union:
SELECT NAME, "IN", HRs
FROM
(
SELECT NAME, "IN", HRs, 1 AS pos FROM Temp
UNION ALL
SELECT NAME, "OUT", HRs, 2 FROM Temp
) t
ORDER BY NAME, pos;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.