I am using SQL Server. Here is the scenario:
Let say I have a tableA:
UserId UserName TransactionType TransactionDateTime
1 Staff1 NULL NULL
2 Staff2 1 2020-08-12 03:11:20.4871383
2 Staff2 2 2020-08-12 03:41:33.4850314
3 Staff3 2 2020-08-12 03:41:33.4848626
3 Staff3 1 2020-08-12 03:11:20.4869688
And I would like to query the result like:
UserId UserName ClockInTime ClockOutTime
1 Staff1 NULL NULL
2 Staff2 2020-08-12 03:11:20.4871383 2020-08-12 03:41:33.4850314
3 Staff3 2020-08-12 03:11:20.4869688 2020-08-12 03:41:33.4848626
So the condition is if type is 1 then the transactiondatetime will be clockintime, if type is 2 then it will be clockouttime.
I tried to use 'case when':
Select UserId, UserName,
case when TransactionType = 1 Then TransactionDateTime else null End as ClockInTime,
case when TransactionType = 2 Then TransactionDateTime else null End as ClockOutTime
From tableA
and in this way it will return 5 lines:
UserId UserName ClockInTime ClockOutTime
1 Staff1 NULL NULL
2 Staff2 2020-08-16 03:11:20.4871383 NULL
2 Staff2 NULL 2020-08-16 03:41:33.4850314
3 Staff3 NULL 2020-08-16 03:41:33.4848626
3 Staff3 2020-08-16 03:11:20.4869688 NULL
Any help is greatly appreciated!!