I need to extract all the rows associated to their respective Ids that have 'In' in the type column that occured before 'Out' using the date column. In the data provided, only Id 1 & 2 would pass the test. Test data is as follows:
CREATE TABLE #Table (
id INT,
[type] varchar(25),
[Dates] Date)
INSERT INTO #Table
VALUES (1, 'In', '2018-10-01'),
(1, 'In', '2018-11-01'),
(1, 'Out', '2018-12-01'),
(2, 'In', '2018-10-01'),
(2, 'Out', '2018-11-01'),
(2, 'In', '2018-12-01'),
(3, 'Out', '2018-10-01'),
(3, 'In', '2018-11-01')
The ouput should look like that:
+----+------+------------+
| id | type | date |
+----+------+------------+
| 1 | In | 2018-10-01 |
| 1 | In | 2018-11-01 |
| 1 | Out | 2018-12-01 |
| 2 | In | 2018-10-01 |
| 2 | Out | 2018-11-01 |
| 2 | In | 2018-12-01 |
+----+------+------------+
Im honestly lost with querying this issue. I started with
SELECT #Table.*, MIN(CASE WHEN #Table.[type] = 'In' THEN #Table.Dates ELSE NULL END) As A
,MIN(CASE WHEN #Table.[type] = 'Out' THEN #Table.Dates ELSE NULL END) As B
FROM #Table
GROUP BY #Table.id, #Table.[type], #Table.Dates
Not sure what to do from there...