Check overlap by each ID, SQL Server 2008 - sql-server

I am trying to find a solution how to find overlap in case below.
I have two tables with employee ID, number or breaktime, hour of planned breaktime start and stop. In ideal situation number of planned break and used should be the same but it is not. What I need to do is compare by each ID and check if used break is in the same time like planned, if not how big is the gap.
My idea is to check by ID each number with every number in the second table and mark it ok/not ok an how big is the gap. I thin there is a better way, more efficient to do this. Could You help me to find the solution?
I'll appreciate every prompt.
ID | number | time_start_plan | time_stop_plan
-----+--------+-------------------------+------------------------
965 | 1 | 2017-09-11 00:00:00.000 | 2017-09-11 00:15:00.000
965 | 2 | 2017-09-11 01:15:00.000 | 2017-09-11 01:25:00.000
965 | 3 | 2017-09-11 02:40:00.000 | 2017-09-11 02:50:00.000
965 | 4 | 2017-09-11 04:20:00.000 | 2017-09-11 04:30:00.000
1122 | 1 | 2017-09-11 00:05:00.000 | 2017-09-11 00:20:00.000
1122 | 2 | 2017-09-11 01:20:00.000 | 2017-09-11 01:30:00.000
1122 | 3 | 2017-09-11 03:10:00.000 | 2017-09-11 03:20:00.000
ID | number | time_start_used | time_stop_used
-----+--------+-------------------------+------------------------
965 | 1 | 2017-09-11 00:34:41.000 | 2017-09-11 00:36:34.000
965 | 2 | 2017-09-11 02:33:00.000 | 2017-09-11 02:36:26.000
965 | 3 | 2017-09-11 04:24:17.000 | 2017-09-11 04:27:42.000
965 | 4 | 2017-09-11 06:06:02.000 | 2017-09-11 06:18:19.000
965 | 5 | 2017-09-11 22:41:02.000 | 2017-09-11 22:42:06.000
1122 | 1 | 2017-09-11 00:15:12.000 | 2017-09-11 00:15:32.000
1122 | 2 | 2017-09-11 01:07:56.000 | 2017-09-11 01:26:57.000
1122 | 3 | 2017-09-11 01:49:02.000 | 2017-09-11 01:51:13.000
1122 | 4 | 2017-09-11 03:33:50.000 | 2017-09-11 03:34:17.000
1122 | 5 | 2017-09-11 04:07:59.000 | 2017-09-11 04:09:10.000
1122 | 6 | 2017-09-11 05:51:23.000 | 2017-09-11 05:54:22.000

This solution variant only partially matching [number] in that the t1.number must be <= to the t2.number. This leads of course to many more rows of output.
See this SQL Fiddle as a demonstration.
Query:
select
coalesce(t1.id, t2.id) ids
, coalesce(t1.number, t2.number) numbers
, datediff(minute,t1.time_start_plan, t2.time_start_used) diff_start
, datediff(minute,t1.time_stop_plan, t2.time_stop_used) diff_stop
, t1.time_start_plan
, t2.time_start_used
, t1.time_stop_plan
, t2.time_stop_used
from table1 t1
LEFT outer join table2 t2 on t1.id = t2.id and t1.number <= t2.number
order by ids, numbers
Results:
| ids | numbers | diff_start | diff_stop | time_start_plan | time_start_used | time_stop_plan | time_stop_used |
|------|---------|------------|-----------|----------------------|----------------------|----------------------|----------------------|
| 965 | 1 | 34 | 21 | 2017-09-11T00:00:00Z | 2017-09-11T00:34:41Z | 2017-09-11T00:15:00Z | 2017-09-11T00:36:34Z |
| 965 | 1 | 153 | 141 | 2017-09-11T00:00:00Z | 2017-09-11T02:33:00Z | 2017-09-11T00:15:00Z | 2017-09-11T02:36:26Z |
| 965 | 1 | 264 | 252 | 2017-09-11T00:00:00Z | 2017-09-11T04:24:17Z | 2017-09-11T00:15:00Z | 2017-09-11T04:27:42Z |
| 965 | 1 | 366 | 363 | 2017-09-11T00:00:00Z | 2017-09-11T06:06:02Z | 2017-09-11T00:15:00Z | 2017-09-11T06:18:19Z |
| 965 | 1 | 1361 | 1347 | 2017-09-11T00:00:00Z | 2017-09-11T22:41:02Z | 2017-09-11T00:15:00Z | 2017-09-11T22:42:06Z |
| 965 | 2 | 78 | 71 | 2017-09-11T01:15:00Z | 2017-09-11T02:33:00Z | 2017-09-11T01:25:00Z | 2017-09-11T02:36:26Z |
| 965 | 2 | 189 | 182 | 2017-09-11T01:15:00Z | 2017-09-11T04:24:17Z | 2017-09-11T01:25:00Z | 2017-09-11T04:27:42Z |
| 965 | 2 | 291 | 293 | 2017-09-11T01:15:00Z | 2017-09-11T06:06:02Z | 2017-09-11T01:25:00Z | 2017-09-11T06:18:19Z |
| 965 | 2 | 1286 | 1277 | 2017-09-11T01:15:00Z | 2017-09-11T22:41:02Z | 2017-09-11T01:25:00Z | 2017-09-11T22:42:06Z |
| 965 | 3 | 104 | 97 | 2017-09-11T02:40:00Z | 2017-09-11T04:24:17Z | 2017-09-11T02:50:00Z | 2017-09-11T04:27:42Z |
| 965 | 3 | 206 | 208 | 2017-09-11T02:40:00Z | 2017-09-11T06:06:02Z | 2017-09-11T02:50:00Z | 2017-09-11T06:18:19Z |
| 965 | 3 | 1201 | 1192 | 2017-09-11T02:40:00Z | 2017-09-11T22:41:02Z | 2017-09-11T02:50:00Z | 2017-09-11T22:42:06Z |
| 965 | 4 | 106 | 108 | 2017-09-11T04:20:00Z | 2017-09-11T06:06:02Z | 2017-09-11T04:30:00Z | 2017-09-11T06:18:19Z |
| 965 | 4 | 1101 | 1092 | 2017-09-11T04:20:00Z | 2017-09-11T22:41:02Z | 2017-09-11T04:30:00Z | 2017-09-11T22:42:06Z |
| 1122 | 1 | 10 | -5 | 2017-09-11T00:05:00Z | 2017-09-11T00:15:12Z | 2017-09-11T00:20:00Z | 2017-09-11T00:15:32Z |
| 1122 | 1 | 62 | 66 | 2017-09-11T00:05:00Z | 2017-09-11T01:07:56Z | 2017-09-11T00:20:00Z | 2017-09-11T01:26:57Z |
| 1122 | 1 | 104 | 91 | 2017-09-11T00:05:00Z | 2017-09-11T01:49:02Z | 2017-09-11T00:20:00Z | 2017-09-11T01:51:13Z |
| 1122 | 1 | 208 | 194 | 2017-09-11T00:05:00Z | 2017-09-11T03:33:50Z | 2017-09-11T00:20:00Z | 2017-09-11T03:34:17Z |
| 1122 | 1 | 242 | 229 | 2017-09-11T00:05:00Z | 2017-09-11T04:07:59Z | 2017-09-11T00:20:00Z | 2017-09-11T04:09:10Z |
| 1122 | 1 | 346 | 334 | 2017-09-11T00:05:00Z | 2017-09-11T05:51:23Z | 2017-09-11T00:20:00Z | 2017-09-11T05:54:22Z |
| 1122 | 2 | -13 | -4 | 2017-09-11T01:20:00Z | 2017-09-11T01:07:56Z | 2017-09-11T01:30:00Z | 2017-09-11T01:26:57Z |
| 1122 | 2 | 29 | 21 | 2017-09-11T01:20:00Z | 2017-09-11T01:49:02Z | 2017-09-11T01:30:00Z | 2017-09-11T01:51:13Z |
| 1122 | 2 | 133 | 124 | 2017-09-11T01:20:00Z | 2017-09-11T03:33:50Z | 2017-09-11T01:30:00Z | 2017-09-11T03:34:17Z |
| 1122 | 2 | 167 | 159 | 2017-09-11T01:20:00Z | 2017-09-11T04:07:59Z | 2017-09-11T01:30:00Z | 2017-09-11T04:09:10Z |
| 1122 | 2 | 271 | 264 | 2017-09-11T01:20:00Z | 2017-09-11T05:51:23Z | 2017-09-11T01:30:00Z | 2017-09-11T05:54:22Z |
| 1122 | 3 | -81 | -89 | 2017-09-11T03:10:00Z | 2017-09-11T01:49:02Z | 2017-09-11T03:20:00Z | 2017-09-11T01:51:13Z |
| 1122 | 3 | 23 | 14 | 2017-09-11T03:10:00Z | 2017-09-11T03:33:50Z | 2017-09-11T03:20:00Z | 2017-09-11T03:34:17Z |
| 1122 | 3 | 57 | 49 | 2017-09-11T03:10:00Z | 2017-09-11T04:07:59Z | 2017-09-11T03:20:00Z | 2017-09-11T04:09:10Z |
| 1122 | 3 | 161 | 154 | 2017-09-11T03:10:00Z | 2017-09-11T05:51:23Z | 2017-09-11T03:20:00Z | 2017-09-11T05:54:22Z |

I think you will need a FULL OUTER JOIN like this example also available here at SQL Fiddle
CREATE TABLE Table1
([ID] int, [number] int, [time_start_plan] datetime, [time_stop_plan] datetime)
;
INSERT INTO Table1
([ID], [number], [time_start_plan], [time_stop_plan])
VALUES
(965, 1, '2017-09-11 00:00:00.000', '2017-09-11 00:15:00.000'),
(965, 2, '2017-09-11 01:15:00.000', '2017-09-11 01:25:00.000'),
(965, 3, '2017-09-11 02:40:00.000', '2017-09-11 02:50:00.000'),
(965, 4, '2017-09-11 04:20:00.000', '2017-09-11 04:30:00.000'),
(1122, 1, '2017-09-11 00:05:00.000', '2017-09-11 00:20:00.000'),
(1122, 2, '2017-09-11 01:20:00.000', '2017-09-11 01:30:00.000'),
(1122, 3, '2017-09-11 03:10:00.000', '2017-09-11 03:20:00.000');
CREATE TABLE Table2
([ID] int, [number] int, [time_start_used] datetime, [time_stop_used] datetime)
;
INSERT INTO Table2
([ID], [number], [time_start_used], [time_stop_used])
VALUES
(965, 1, '2017-09-11 00:34:41.000', '2017-09-11 00:36:34.000'),
(965, 2, '2017-09-11 02:33:00.000', '2017-09-11 02:36:26.000'),
(965, 3, '2017-09-11 04:24:17.000', '2017-09-11 04:27:42.000'),
(965, 4, '2017-09-11 06:06:02.000', '2017-09-11 06:18:19.000'),
(965, 5, '2017-09-11 22:41:02.000', '2017-09-11 22:42:06.000'),
(1122, 1, '2017-09-11 00:15:12.000', '2017-09-11 00:15:32.000'),
(1122, 2, '2017-09-11 01:07:56.000', '2017-09-11 01:26:57.000'),
(1122, 3, '2017-09-11 01:49:02.000', '2017-09-11 01:51:13.000'),
(1122, 4, '2017-09-11 03:33:50.000', '2017-09-11 03:34:17.000'),
(1122, 5, '2017-09-11 04:07:59.000', '2017-09-11 04:09:10.000'),
(1122, 6, '2017-09-11 05:51:23.000', '2017-09-11 05:54:22.000')
;
Query:
select
coalesce(t1.id, t2.id) ids
, coalesce(t1.number, t2.number) numbers
, datediff(minute,t1.time_start_plan, t2.time_start_used) diff_start
, datediff(minute,t1.time_stop_plan, t2.time_stop_used) diff_stop
, t1.time_start_plan
, t2.time_start_used
, t1.time_stop_plan
, t2.time_stop_used
from table1 t1
full outer join table2 t2 on t1.id = t2.id and t1.number = t2.number
order by ids, numbers
Results:
| ids | numbers | diff_start | diff_stop | time_start_plan | time_start_used | time_stop_plan | time_stop_used |
|------|---------|------------|-----------|----------------------|----------------------|----------------------|----------------------|
| 965 | 1 | 34 | 21 | 2017-09-11T00:00:00Z | 2017-09-11T00:34:41Z | 2017-09-11T00:15:00Z | 2017-09-11T00:36:34Z |
| 965 | 2 | 78 | 71 | 2017-09-11T01:15:00Z | 2017-09-11T02:33:00Z | 2017-09-11T01:25:00Z | 2017-09-11T02:36:26Z |
| 965 | 3 | 104 | 97 | 2017-09-11T02:40:00Z | 2017-09-11T04:24:17Z | 2017-09-11T02:50:00Z | 2017-09-11T04:27:42Z |
| 965 | 4 | 106 | 108 | 2017-09-11T04:20:00Z | 2017-09-11T06:06:02Z | 2017-09-11T04:30:00Z | 2017-09-11T06:18:19Z |
| 965 | 5 | (null) | (null) | (null) | 2017-09-11T22:41:02Z | (null) | 2017-09-11T22:42:06Z |
| 1122 | 1 | 10 | -5 | 2017-09-11T00:05:00Z | 2017-09-11T00:15:12Z | 2017-09-11T00:20:00Z | 2017-09-11T00:15:32Z |
| 1122 | 2 | -13 | -4 | 2017-09-11T01:20:00Z | 2017-09-11T01:07:56Z | 2017-09-11T01:30:00Z | 2017-09-11T01:26:57Z |
| 1122 | 3 | -81 | -89 | 2017-09-11T03:10:00Z | 2017-09-11T01:49:02Z | 2017-09-11T03:20:00Z | 2017-09-11T01:51:13Z |
| 1122 | 4 | (null) | (null) | (null) | 2017-09-11T03:33:50Z | (null) | 2017-09-11T03:34:17Z |
| 1122 | 5 | (null) | (null) | (null) | 2017-09-11T04:07:59Z | (null) | 2017-09-11T04:09:10Z |
| 1122 | 6 | (null) | (null) | (null) | 2017-09-11T05:51:23Z | (null) | 2017-09-11T05:54:22Z |

Related

How to net off amount column with same value and different sign from single table using sql server

Below is the dataset. I am trying to net off the value like 59 with -59 and leave with value that is not getting net off.
I tried joining with same table as per below query but didn't get expected output
+--------+------------+----------+---------+--------+----------+-------+
| Txn_ID | Price_Type | Price_ID | Country | Center | Amount | DR_CR |
+--------+------------+----------+---------+--------+----------+-------+
| 1 | 2225 | 439914 | USA | 3602 | 59 | Cr |
| 2 | 2225 | 439584 | USA | 3602 | 157.53 | Cr |
| 3 | 2225 | 439292 | USA | 3602 | 59 | Cr |
| 4 | 2225 | 439816 | USA | 3602 | 59 | Cr |
| 5 | 2225 | 439546 | USA | 6313 | -497.25 | Dr |
| 6 | 2225 | 439949 | USA | 6313 | 1953.15 | Cr |
| 7 | 2225 | 439546 | USA | 3602 | 59 | Cr |
| 8 | 2225 | 439949 | USA | 3602 | 25 | Cr |
| 9 | 1083 | 182221 | USA | 7851 | -59 | Dr |
| 10 | 2225 | 438689 | USA | 3602 | 59 | Cr |
| 11 | 2225 | 438689 | USA | 6313 | -415.88 | Dr |
| 12 | 2225 | 438965 | USA | 3602 | 59 | Cr |
| 13 | 1083 | 182257 | USA | 7851 | -59 | Dr |
| 14 | 1083 | 182251 | USA | 7851 | -59 | Dr |
| 15 | 2225 | 439844 | USA | 3602 | 25 | Cr |
| 16 | 1083 | 182265 | USA | 7851 | -1978.15 | Dr |
| 17 | 1083 | 182230 | USA | 7851 | -25 | Dr |
| 18 | 1069 | 23888 | USA | 7851 | 356.88 | Cr |
| 19 | 1083 | 182225 | USA | 7851 | 438.25 | Cr |
+--------+------------+----------+---------+--------+----------+-------+
Query tried to check if expected rows are getting matched :
SELECT T1.,T2. FROM Product T1
INNER JOIN Product T2
ON T1.DR_CR<>T2.DR_CR
AND ABS(T1.Amount)=ABS(T2.Amount)
AND T1.Txn_ID<>T2.Txn_ID
Please can anyone suggest any way to netoff the amount value?
Expected result as per below:
+--------+------------+----------+---------+--------+----------+-------+
| Txn_ID | Price_Type | Price_ID | Country | Center | Amount | DR_CR |
+--------+------------+----------+---------+--------+----------+-------+
| 2 | 2225 | 439584 | USA | 3602 | 157.53 | Cr |
| 5 | 2225 | 439546 | USA | 6313 | -497.25 | Dr |
| 6 | 2225 | 439949 | USA | 6313 | 1953.15 | Cr |
| 7 | 2225 | 439546 | USA | 3602 | 59 | Cr |
| 10 | 2225 | 438689 | USA | 3602 | 59 | Cr |
| 11 | 2225 | 438689 | USA | 6313 | -415.88 | Dr |
| 12 | 2225 | 438965 | USA | 3602 | 59 | Cr |
| 15 | 2225 | 439844 | USA | 3602 | 25 | Cr |
| 16 | 1083 | 182265 | USA | 7851 | -1978.15 | Dr |
| 18 | 1069 | 23888 | USA | 7851 | 356.88 | Cr |
| 19 | 1083 | 182225 | USA | 7851 | 438.25 | Cr |
+--------+------------+----------+---------+--------+----------+-------+
I have found the answer after hitting trail and error method.
Below query has partition and assigned the rank to amount and Dr_cr column wise. Then I got required output by joining table with itself.
;WITH CTE AS (
select Txn_ID
,Price_Type
,Price_ID
,Country
,Center
,Amount
,DR_CR
,RANK() over (partition by ABS(Amount),DR_CR order by CASE WHEN DR_CR,Txn_ID ) RNK FROM Product),
select T1.*
from CTE t1
inner join CTE t2 on t1.abs(Amount)=t2.abs(Amount)
and t1.DR_CR<>t2.DR_CR
and t1.RNK=t2.RNK

Grouping Data by Changing Status Over Time

I am trying to assign a group number to distinct groups of rows in a dataset that has changing data over time. The changing fields are tran_seq, prog_id, deg-id, cur_id, and enroll_status in my example. When any of those fields are different from the previous row, I need a new grouping number. When the fields are the same as the prior row, then the grouping number should stay the same. When I try ROW_NUMBER(), RANK(), or DENSE_RANK(), I get increasing values for the same group (e.g. the first 2 rows in example). I feel I need to ORDER BY start_date as it is temporal data.
+----+----------+---------+--------+--------+---------------+------------+------------+---------+
| | tran_seq | prog_id | deg_id | cur_id | enroll_status | start_date | end_date | desired |
+----+----------+---------+--------+--------+---------------+------------+------------+---------+
| 1 | 1 | 6 | 9 | 3 | ENRL | 2004-08-22 | 2004-12-11 | 1 |
| 2 | 1 | 6 | 9 | 3 | ENRL | 2006-01-10 | 2006-05-06 | 1 |
| 3 | 1 | 6 | 9 | 59 | ENRL | 2006-08-29 | 2006-12-16 | 2 |
| 4 | 2 | 12 | 23 | 45 | ENRL | 2014-01-21 | 2014-05-16 | 3 |
| 5 | 2 | 12 | 23 | 45 | ENRL | 2014-08-18 | 2014-12-05 | 3 |
| 6 | 2 | 12 | 23 | 45 | LOAP | 2015-01-20 | 2015-05-15 | 4 |
| 7 | 2 | 12 | 23 | 45 | ENRL | 2015-08-25 | 2015-12-11 | 5 |
| 8 | 2 | 12 | 23 | 45 | LOAP | 2016-01-12 | 2016-05-06 | 6 |
| 9 | 2 | 12 | 23 | 45 | ENRL | 2016-05-16 | 2016-08-05 | 7 |
| 10 | 2 | 12 | 23 | 45 | LOAJ | 2016-08-23 | 2016-12-02 | 8 |
| 11 | 2 | 12 | 23 | 45 | ENRL | 2017-01-18 | 2017-05-05 | 9 |
| 12 | 2 | 12 | 23 | 45 | ENRL | 2018-01-17 | 2018-05-11 | 9 |
+----+----------+---------+--------+--------+---------------+------------+------------+---------+
Once I have grouping numbers, I think I can group by those to get what I'm ultimately after: a timeline of different statuses with start dates and end dates. For the example data above, that would be:
+---+----------+---------+--------+--------+---------------+------------+------------+
| | tran_seq | prog_id | deg_id | cur_id | enroll_status | start_date | end_date |
+---+----------+---------+--------+--------+---------------+------------+------------+
| 1 | 1 | 6 | 9 | 3 | ENRL | 2004-08-22 | 2006-05-06 |
| 2 | 1 | 6 | 9 | 59 | ENRL | 2004-08-29 | 2006-12-16 |
| 3 | 2 | 12 | 23 | 45 | ENRL | 2014-01-21 | 2014-12-05 |
| 4 | 2 | 12 | 23 | 45 | LOAP | 2015-01-20 | 2015-05-15 |
| 5 | 2 | 12 | 23 | 45 | ENRL | 2015-08-25 | 2015-12-11 |
| 6 | 2 | 12 | 23 | 45 | LOAP | 2016-01-12 | 2016-05-06 |
| 7 | 2 | 12 | 23 | 45 | ENRL | 2016-05-16 | 2016-08-05 |
| 8 | 2 | 12 | 23 | 45 | LOAJ | 2016-08-23 | 2016-12-02 |
| 9 | 2 | 12 | 23 | 45 | ENRL | 2017-01-17 | 2018-05-06 |
+---+----------+---------+--------+--------+---------------+------------+------------+
This is a classic XY problem, in that you are asking for an intermediate step to a different solution, rather than asking about the solution itself.
As you included your overall end goal as a bit of an addendum however, here is how you can reach that without your intermediate step:
declare #t table(tran_seq int, prog_id int, deg_id int, cur_id int, enroll_status varchar(4), start_date date, end_date date, desired int)
insert into #t values
(1,6,9,3 ,'ENRL','2004-08-22','2004-12-11',1)
,(1,6,9,3 ,'ENRL','2006-01-10','2006-05-06',1)
,(1,6,9,59 ,'ENRL','2006-08-29','2006-12-16',2)
,(2,12,23,45,'ENRL','2014-01-21','2014-05-16',3)
,(2,12,23,45,'ENRL','2014-08-18','2014-12-05',3)
,(2,12,23,45,'LOAP','2015-01-20','2015-05-15',4)
,(2,12,23,45,'ENRL','2015-08-25','2015-12-11',5)
,(2,12,23,45,'LOAP','2016-01-12','2016-05-06',6)
,(2,12,23,45,'ENRL','2016-05-16','2016-08-05',7)
,(2,12,23,45,'LOAJ','2016-08-23','2016-12-02',8)
,(2,12,23,45,'ENRL','2017-01-18','2017-05-05',9)
,(2,12,23,45,'ENRL','2018-01-17','2018-05-11',9)
;
select tran_seq
,prog_id
,deg_id
,cur_id
,enroll_status
,min(start_date) as start_date
,max(end_date) as end_date
from(select *
,row_number() over (order by end_date) - row_number() over (partition by tran_seq,prog_id,deg_id,cur_id,enroll_status order by end_date) as grp
from #t
) AS g
group by tran_seq
,prog_id
,deg_id
,cur_id
,enroll_status
,grp
order by start_date;
Output
+----------+---------+--------+--------+---------------+------------+------------+
| tran_seq | prog_id | deg_id | cur_id | enroll_status | start_date | end_date |
+----------+---------+--------+--------+---------------+------------+------------+
| 1 | 6 | 9 | 3 | ENRL | 2004-08-22 | 2006-05-06 |
| 1 | 6 | 9 | 59 | ENRL | 2006-08-29 | 2006-12-16 |
| 2 | 12 | 23 | 45 | ENRL | 2014-01-21 | 2014-12-05 |
| 2 | 12 | 23 | 45 | LOAP | 2015-01-20 | 2015-05-15 |
| 2 | 12 | 23 | 45 | ENRL | 2015-08-25 | 2015-12-11 |
| 2 | 12 | 23 | 45 | LOAP | 2016-01-12 | 2016-05-06 |
| 2 | 12 | 23 | 45 | ENRL | 2016-05-16 | 2016-08-05 |
| 2 | 12 | 23 | 45 | LOAJ | 2016-08-23 | 2016-12-02 |
| 2 | 12 | 23 | 45 | ENRL | 2017-01-18 | 2018-05-11 |
+----------+---------+--------+--------+---------------+------------+------------+

SQL Server Merge rows using lag?

i have a table that , have Intime, OutTime, zonechk flag
if 1 mean inside , null mean outside , i need to merge them for reporting Propose for example
inside 10:10 to 10:40
inside 11:10 to 12:30
outside 12:31 to 14:00
inside 14:10 to 14:40
inside 15:10 to 15:30
to be
inside 10:10 to 12:30
outside 12:31 to 14:00
inside 14:10 to 15:40
+--------+--------------+----------------+--------------+----------+-------------------------+--------+---------+
| Number | plate_number | move_from_date | move_to_date | Duration | inTime | zoneid | zonechk |
+--------+--------------+----------------+--------------+----------+-------------------------+--------+---------+
| 5934 | NULL | 1507713204 | 1508147403 | 434199 | 2017-10-11 09:13:24.100 | 54 | 1 |
| 5934 | NULL | 1507793515 | 1507793924 | 409 | 2017-10-12 07:31:55.310 | 709 | NULL |
| 5934 | NULL | 1508051465 | 1508051861 | 396 | 2017-10-15 07:11:05.010 | 709 | 1 |
| 5934 | NULL | 1508139025 | 1508139551 | 526 | 2017-10-16 07:30:25.570 | 709 | 1 |
| 5934 | NULL | 1508147732 | 1508148337 | 605 | 2017-10-16 09:55:32.650 | 698 | NULL |
| 5934 | NULL | 1508148802 | 1508235374 | 86572 | 2017-10-16 10:13:22.060 | 54 | 1 |
| 5934 | NULL | 1508227127 | 1508227707 | 580 | 2017-10-17 07:58:47.040 | 709 | NULL |
| 5934 | NULL | 1508235727 | 1508236062 | 335 | 2017-10-17 10:22:07.150 | 698 | 1 |
| 5934 | NULL | 1508236426 | 1508320755 | 84329 | 2017-10-17 10:33:46.040 | 54 | 1 |
| 5934 | NULL | 1508312035 | 1508312473 | 438 | 2017-10-18 07:33:55.780 | 709 | 1 |
| 5934 | NULL | 1508321089 | 1508321767 | 678 | 2017-10-18 10:04:49.110 | 698 | 1 |
| 5934 | NULL | 1508322313 | 1508405959 | 83646 | 2017-10-18 10:25:13.990 | 54 | 1 |
| 5934 | NULL | 1508398316 | 1508398778 | 462 | 2017-10-19 07:31:56.680 | 709 | 1 |
| 5934 | NULL | 1508406309 | 1508406811 | 502 | 2017-10-19 09:45:09.260 | 698 | NULL |
| 5934 | NULL | 1508407153 | 1508575805 | 168652 | 2017-10-19 09:59:13.857 | 54 | 1 |
| 5934 | NULL | 1508565909 | 1508566376 | 467 | 2017-10-21 06:05:09.240 | 709 | NULL |
| 5934 | NULL | 1508576176 | 1508576601 | 425 | 2017-10-21 08:56:16.473 | 698 | 1 |
| 5934 | NULL | 1508577010 | 1508834353 | 257343 | 2017-10-21 09:10:10.710 | 54 | 1 |
| 5934 | NULL | 1508584759 | 1508585455 | 696 | 2017-10-21 11:19:19.930 | 709 | NULL |
| 5934 | NULL | 1508659492 | 1508660021 | 529 | 2017-10-22 08:04:52.490 | 709 | 1 |
| 5934 | NULL | 1508741980 | 1508742462 | 482 | 2017-10-23 06:59:40.960 | 709 | 1 |
| 5934 | NULL | 1508749508 | 1508750050 | 542 | 2017-10-23 09:05:08.893 | 709 | NULL |
| 5934 | NULL | 1508834742 | 1508835409 | 667 | 2017-10-24 08:45:42.960 | 698 | 1 |
| 5934 | NULL | 1508917280 | 1508917694 | 414 | 2017-10-25 07:41:20.940 | 709 | 1 |
| 5934 | NULL | 1508966861 | 1509000828 | 33967 | 2017-10-25 21:27:41.490 | 54 | 1 |
| 5934 | NULL | 1509000911 | 1509002958 | 2047 | 2017-10-26 06:55:11.330 | 54 | 1 |
| 5934 | NULL | 1509001844 | 1509002376 | 532 | 2017-10-26 07:10:44.090 | 709 | NULL |
| 5934 | NULL | 1509003211 | 1509007113 | 3902 | 2017-10-26 07:33:31.023 | 54 | 1 |
| 5934 | NULL | 1509006789 | 1509006863 | 74 | 2017-10-26 08:33:09.350 | 54 | 1 |
| 5934 | NULL | 1509006866 | 1509007049 | 183 | 2017-10-26 08:34:26.130 | 54 | 1 |
| 5934 | NULL | 1509007257 | 1509009354 | 2097 | 2017-10-26 08:40:57.170 | 54 | 1 |
| 5934 | NULL | 1509008978 | 1509009089 | 111 | 2017-10-26 09:09:38.660 | 54 | 1 |
| 5934 | NULL | 1509009092 | 1509009231 | 139 | 2017-10-26 09:11:32.420 | 54 | 1 |
| 5934 | NULL | 1509009458 | 1509009946 | 488 | 2017-10-26 09:17:38.590 | 54 | 1 |
| 5934 | NULL | 1509009529 | 1509009608 | 79 | 2017-10-26 09:18:49.600 | 54 | 1 |
| 5934 | NULL | 1509009610 | 1509009757 | 147 | 2017-10-26 09:20:10.990 | 54 | 1 |
| 5934 | NULL | 1509009759 | 1509009909 | 150 | 2017-10-26 09:22:39.610 | 54 | 1 |
| 5934 | NULL | 1509010472 | 1509010787 | 315 | 2017-10-26 09:34:32.270 | 698 | 1 |
| 5934 | NULL | 1509011192 | 1509011367 | 175 | 2017-10-26 09:46:32.530 | 54 | 1 |
| 5934 | NULL | 1509011370 | 1509011424 | 54 | 2017-10-26 09:49:30.100 | 54 | 1 |
| 5934 | NULL | 1509011644 | 1509016547 | 4903 | 2017-10-26 09:54:04.390 | 54 | 1 |
| 5934 | NULL | 1509016793 | 1509018783 | 1990 | 2017-10-26 11:19:53.370 | 54 | 1 |
| 5934 | NULL | 1509018880 | 1509021232 | 2352 | 2017-10-26 11:54:40.680 | 54 | 1 |
| 5934 | NULL | 1509021327 | 1509041423 | 20096 | 2017-10-26 12:35:27.760 | 54 | 1 |
| 5934 | NULL | 1509042027 | 1509179109 | 137082 | 2017-10-26 18:20:27.460 | 54 | 1 |
| 5934 | NULL | 1509174809 | 1509175309 | 500 | 2017-10-28 07:13:29.010 | 709 | NULL |
| 5934 | NULL | 1509179335 | 1509179907 | 572 | 2017-10-28 08:28:55.660 | 54 | 1 |
| 5934 | NULL | 1509180005 | 1509180828 | 823 | 2017-10-28 08:40:05.890 | 54 | 1 |
| 5934 | NULL | 1509180925 | 1509187555 | 6630 | 2017-10-28 08:55:25.270 | 54 | 1 |
| 5934 | NULL | 1509187624 | 1509189851 | 2227 | 2017-10-28 10:47:04.630 | 54 | 1 |
| 5934 | NULL | 1509190143 | 1509190301 | 158 | 2017-10-28 11:29:03.580 | 54 | 1 |
| 5934 | NULL | 1509190387 | 1509191479 | 1092 | 2017-10-28 11:33:07.230 | 54 | 1 |
| 5934 | NULL | 1509191575 | 1509192224 | 649 | 2017-10-28 11:52:55.150 | 54 | 1 |
| 5934 | NULL | 1509192314 | 1509192479 | 165 | 2017-10-28 12:05:14.800 | 54 | 1 |
| 5934 | NULL | 1509192588 | 1509218423 | 25835 | 2017-10-28 12:09:48.670 | 54 | 1 |
| 5934 | NULL | 1509219048 | 1509224551 | 5503 | 2017-10-28 19:30:48.440 | 54 | 1 |
| 5934 | NULL | 1509225176 | 1509260681 | 35505 | 2017-10-28 21:12:56.600 | 54 | 1 |
| 5934 | NULL | 1509260770 | 1509269210 | 8440 | 2017-10-29 07:06:10.840 | 54 | 1 |
| 5934 | NULL | 1509261874 | 1509262382 | 508 | 2017-10-29 07:24:34.730 | 709 | NULL |
| 5934 | NULL | 1509269612 | 1509270116 | 504 | 2017-10-29 09:33:32.580 | 698 | 1 |
| 5934 | NULL | 1509270123 | 1509270136 | 13 | 2017-10-29 09:42:03.510 | 698 | 1 |
| 5934 | NULL | 1509270499 | 1509271818 | 1319 | 2017-10-29 09:48:19.940 | 54 | 1 |
| 5934 | NULL | 1509271880 | 1509272029 | 149 | 2017-10-29 10:11:20.030 | 54 | 1 |
| 5934 | NULL | 1509339400 | 1509348800 | 9400 | 2017-10-30 04:56:40.890 | 54 | 1 |
| 5934 | NULL | 1509348868 | 1509350133 | 1265 | 2017-10-30 07:34:28.720 | 54 | 1 |
| 5934 | NULL | 1509349430 | 1509349892 | 462 | 2017-10-30 07:43:50.690 | 709 | 1 |
| 5934 | NULL | 1509350202 | 1509352410 | 2208 | 2017-10-30 07:56:42.050 | 54 | 1 |
| 5934 | NULL | 1509352779 | 1509353737 | 958 | 2017-10-30 08:39:39.040 | 54 | 1 |
| 5934 | NULL | 1509353800 | 1509354320 | 520 | 2017-10-30 08:56:40.150 | 54 | 1 |
| 5934 | NULL | 1509354782 | 1509358601 | 3819 | 2017-10-30 09:13:02.250 | 54 | 1 |
| 5934 | NULL | 1509359418 | 1509432505 | 73087 | 2017-10-30 10:30:18.960 | 54 | 1 |
| 5934 | NULL | 1509432786 | 1509432917 | 131 | 2017-10-31 06:53:06.880 | 54 | 1 |
| 5934 | NULL | 1509432999 | 1509433705 | 706 | 2017-10-31 06:56:39.133 | 54 | 1 |
| 5934 | NULL | 1509433790 | 1509438679 | 4889 | 2017-10-31 07:09:50.570 | 54 | 1 |
| 5934 | NULL | 1509438792 | 1509438859 | 67 | 2017-10-31 08:33:12.063 | 54 | 1 |
| 5934 | NULL | 1509438962 | 1509441624 | 2662 | 2017-10-31 08:36:02.810 | 54 | 1 |
| 5934 | NULL | 1509443082 | 1509443985 | 903 | 2017-10-31 09:44:42.670 | 54 | 1 |
| 5934 | NULL | 1509443897 | 1509443930 | 33 | 2017-10-31 09:58:17.230 | 54 | 1 |
| 5934 | NULL | 1509444103 | 1509447041 | 2938 | 2017-10-31 10:01:43.537 | 54 | 1 |
| 5934 | NULL | 1509447618 | 1509450979 | 3361 | 2017-10-31 11:00:18.440 | 54 | 1 |
| 5934 | NULL | 1509451066 | 1509451371 | 305 | 2017-10-31 11:57:46.560 | 54 | 1 |
+--------+--------------+----------------+--------------+----------+-------------------------+--------+---------+
how to achieve that using lag with delete in TSQL ? lag must be by number and zoneId with inTime ASC
Using cross apply can be helpful here, e.g.
available as a demo at SQL Fiddle
CREATE TABLE Table1
([Number] int, [plate_number] varchar(4), [move_from_date] int, [move_to_date] int, [Duration] int, [inTime] datetime, [zoneid] int, [zonechk] varchar(4))
;
INSERT INTO Table1
([Number], [plate_number], [move_from_date], [move_to_date], [Duration], [inTime], [zoneid], [zonechk])
VALUES
(5934, NULL, 1507713204, 1508147403, 434199, '2017-10-11 09:13:24', 54, '1'),
(5934, NULL, 1507793515, 1507793924, 409, '2017-10-12 07:31:55', 709, NULL),
(5934, NULL, 1508051465, 1508051861, 396, '2017-10-15 07:11:05', 709, '1'),
(5934, NULL, 1508139025, 1508139551, 526, '2017-10-16 07:30:25', 709, '1'),
(5934, NULL, 1508147732, 1508148337, 605, '2017-10-16 09:55:32', 698, NULL),
(5934, NULL, 1508148802, 1508235374, 86572, '2017-10-16 10:13:22', 54, '1'),
(5934, NULL, 1508227127, 1508227707, 580, '2017-10-17 07:58:47', 709, NULL),
(5934, NULL, 1508235727, 1508236062, 335, '2017-10-17 10:22:07', 698, '1')
;
Query 1:
select
*
from table1 t
cross apply (
select top(1) move_to_date as next_move_to
from table1 nxt
where nxt.zonechk IS NULL
and nxt.move_to_date > t.move_from_date
order by nxt.move_from_date, nxt.move_to_date
) ca
where zonechk = 1
Results:
| Number | plate_number | move_from_date | move_to_date | Duration | inTime | zoneid | zonechk | next_move_to |
|--------|--------------|----------------|--------------|----------|----------------------|--------|---------|--------------|
| 5934 | (null) | 1507713204 | 1508147403 | 434199 | 2017-10-11T09:13:24Z | 54 | 1 | 1507793924 |
| 5934 | (null) | 1508051465 | 1508051861 | 396 | 2017-10-15T07:11:05Z | 709 | 1 | 1508148337 |
| 5934 | (null) | 1508139025 | 1508139551 | 526 | 2017-10-16T07:30:25Z | 709 | 1 | 1508148337 |
| 5934 | (null) | 1508148802 | 1508235374 | 86572 | 2017-10-16T10:13:22Z | 54 | 1 | 1508227707 |

Trying to create a parent-child hierarchy in sql server

I have a data set that is sorted by account key. when coming to certain rows which have NULL as group key, it should add current account key as a parent key for all above rows which have NULL as parentkey. So when coming to the next row with null as group key, you would set the current account key as parent key for all rows above that have parent key like null.
I have tried to copy a dataset below as mark down table but as you see I can't say I succeeded very well, but I hope some of you can help with the t-sql syntax to create a parent-child hierarchy of this
| AccountKey | ParentKey | GroupKey | AccountNumber | Cat | LineName | LineId |
|------------|-----------|----------|---------------|----------|------------------------------|--------------------------------------|
| 1 | NULL | 7 | 3040 | Account | Salg fisk | C6BCDFB2-1AAC-4D05-94F1-879CDC615D76 |
| 2 | NULL | 7 | 3041 | Account | Salg fisk | C6BCDFB2-1AAC-4D05-94F1-879CDC615D76 |
| 3 | NULL | 7 | 3081 | Account | Salg fisk | C6BCDFB2-1AAC-4D05-94F1-879CDC615D76 |
| 4 | NULL | 7 | 3082 | Account | Salg fisk | C6BCDFB2-1AAC-4D05-94F1-879CDC615D76 |
| 5 | NULL | 7 | 3083 | Account | Salg fisk | C6BCDFB2-1AAC-4D05-94F1-879CDC615D76 |
| 6 | NULL | 7 | 3085 | Account | Salg fisk | C6BCDFB2-1AAC-4D05-94F1-879CDC615D76 |
| 7 | NULL | 7 | 3086 | Account | Salg fisk | C6BCDFB2-1AAC-4D05-94F1-879CDC615D76 |
| 8 | NULL | 7 | 3087 | Account | Salg fisk | C6BCDFB2-1AAC-4D05-94F1-879CDC615D76 |
| 9 | NULL | 2 | 3000 | Account | Salg annet | 26AC86B2-0667-463E-B994-11A5C6D519A6 |
| 10 | NULL | 2 | 3010 | Account | Salg annet | 26AC86B2-0667-463E-B994-11A5C6D519A6 |
| 11 | NULL | 2 | 3020 | Account | Salg annet | 26AC86B2-0667-463E-B994-11A5C6D519A6 |
| 12 | NULL | 2 | 3030 | Account | Salg annet | 26AC86B2-0667-463E-B994-11A5C6D519A6 |
| 41 | NULL | 11 | 3050 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 42 | NULL | 11 | 3600 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 43 | NULL | 11 | 3601 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 44 | NULL | 11 | 3610 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 45 | NULL | 11 | 3615 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 46 | NULL | 11 | 3690 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 47 | NULL | 11 | 3691 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 48 | NULL | 11 | 3701 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 49 | NULL | 11 | 3705 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 50 | NULL | 11 | 3720 | Account | Andre driftsinntekter | 65FFB620-AE42-4BE5-A6E7-BF3339AA04DF |
| 67 | NULL | NULL | NULL | SubTotal | Sum inntekter | NULL |
| 68 | NULL | 13 | 4120 | Account | Innkjøp smolt/settefisk/rogn | F9EE1CE4-22C7-400B-BC9D-E2D3214A5113 |
| 69 | NULL | 10 | 4010 | Account | Vareforbruk fôr | 04E63B6D-CA54-423D-8A44-A4ED99861975 |
| 70 | NULL | 10 | 4901 | Account | Vareforbruk fôr | 04E63B6D-CA54-423D-8A44-A4ED99861975 |
| 71 | NULL | 3 | 4000 | Account | Andre varekostnader | DB7FABAB-7ABA-4B9A-9720-1B538D99B3C8 |
| 72 | NULL | 3 | 4020 | Account | Andre varekostnader | DB7FABAB-7ABA-4B9A-9720-1B538D99B3C8 |
| 73 | NULL | 3 | 4030 | Account | Andre varekostnader | DB7FABAB-7ABA-4B9A-9720-1B538D99B3C8 |
| 133 | NULL | 8 | 4925 | Account | Beholdningsendring fisk | A8BA6F19-A792-44A1-AA21-8F79DB24D224 |
| 134 | NULL | NULL | NULL | SubTotal | Sum varekostnader | NULL |
| 135 | NULL | 12 | 5000 | Account | Lønn og sosiale kostnader | 5C475EDE-3731-4D39-B11A-C8EE72213FF6 |
| 136 | NULL | 12 | 5001 | Account | Lønn og sosiale kostnader | 5C475EDE-3731-4D39-B11A-C8EE72213FF6 |
| 137 | NULL | 12 | 5005 | Account | Lønn og sosiale kostnader | 5C475EDE-3731-4D39-B11A-C8EE72213FF6 |
| 138 | NULL | 12 | 5009 | Account | Lønn og sosiale kostnader | 5C475EDE-3731-4D39-B11A-C8EE72213FF6 |
| 263 | NULL | NULL | NULL | SubTotal | Sum lønnskostnadern | NULL |
| 462 | NULL | NULL | NULL | SubTotal | RESULTAT ETTER SKATT | NULL
If I understood your question you could use:
SELECT Accountkey, ParentKey,GroupKey,AccountNumber, NEW_PARID
FROM (
SELECT Accountkey, ParentKey,GroupKey,AccountNumber, AccountKey AS NEW_PARID, LAG(ACCOUNTKEY) OVER (ORDER BY Accountkey) AS PREC
FROM MYT
WHERE GroupKey IS NULL
UNION ALL
SELECT A.Accountkey, A.ParentKey,A.GroupKey,A.AccountNumber, B.Accountkey AS NEW_PARID, B.PREC
FROM MYT A
INNER JOIN ( SELECT Accountkey, ParentKey,GroupKey,AccountNumber, AccountKey AS NEW_PARID, LAG(ACCOUNTKEY) OVER (ORDER BY Accountkey) AS PREC
FROM MYT
WHERE GroupKey IS NULL) B ON A.Accountkey < B.Accountkey AND (B.PREC IS NULL OR B.PREC<A.accountKey)
WHERE A.GroupKey IS NOT NULL
AND B.GroupKey IS NULL
) X ORDER BY ACCOUNTKEY
You can write it in this way too (it's the same query):
WITH X AS (SELECT Accountkey, ParentKey,GroupKey,AccountNumber, AccountKey AS NEW_PARID, LAG(ACCOUNTKEY) OVER (ORDER BY Accountkey) AS PREC
FROM MYT
WHERE GroupKey IS NULL)
SELECT X.*
FROM X
UNION ALL
SELECT A.Accountkey, A.ParentKey,A.GroupKey,A.AccountNumber, X.Accountkey AS NEW_PARID, X.PREC
FROM MYT A
INNER JOIN X ON A.Accountkey < X.Accountkey AND (X.PREC IS NULL OR X.PREC<A.accountKey)
WHERE A.GroupKey IS NOT NULL
Output (MYT is the name of the table, the new parentid column is NEW_PARID):
+------------+-----------+----------+---------------+-----------+
| Accountkey | ParentKey | GroupKey | AccountNumber | NEW_PARID |
+------------+-----------+----------+---------------+-----------+
| 1 | NULL | 7 | 3040 | 67 |
| 2 | NULL | 7 | 3041 | 67 |
| 3 | NULL | 7 | 3081 | 67 |
| 4 | NULL | 7 | 3082 | 67 |
| 5 | NULL | 7 | 3083 | 67 |
| 6 | NULL | 7 | 3085 | 67 |
| 7 | NULL | 7 | 3086 | 67 |
| 8 | NULL | 7 | 3087 | 67 |
| 9 | NULL | 2 | 3000 | 67 |
| 10 | NULL | 2 | 3010 | 67 |
| 11 | NULL | 2 | 3020 | 67 |
| 12 | NULL | 2 | 3030 | 67 |
| 41 | NULL | 11 | 3050 | 67 |
| 42 | NULL | 11 | 3600 | 67 |
| 43 | NULL | 11 | 3601 | 67 |
| 44 | NULL | 11 | 3610 | 67 |
| 45 | NULL | 11 | 3615 | 67 |
| 46 | NULL | 11 | 3690 | 67 |
| 47 | NULL | 11 | 3691 | 67 |
| 48 | NULL | 11 | 3701 | 67 |
| 49 | NULL | 11 | 3705 | 67 |
| 50 | NULL | 11 | 3720 | 67 |
| 67 | NULL | NULL | NULL | 67 |
| 68 | NULL | 13 | 4120 | 134 |
| 69 | NULL | 10 | 4010 | 134 |
| 70 | NULL | 10 | 4901 | 134 |
| 71 | NULL | 3 | 4000 | 134 |
| 72 | NULL | 3 | 4020 | 134 |
| 73 | NULL | 3 | 4030 | 134 |
| 133 | NULL | 8 | 4925 | 134 |
| 134 | NULL | NULL | NULL | 134 |
| 135 | NULL | 12 | 5000 | 263 |
| 136 | NULL | 12 | 5001 | 263 |
| 137 | NULL | 12 | 5005 | 263 |
| 138 | NULL | 12 | 5009 | 263 |
| 263 | NULL | NULL | NULL | 263 |
| 462 | NULL | NULL | NULL | 462 |
+------------+-----------+----------+---------------+-----------+
Updated 20171221 - for MSSQL 2008
You can try this (but pay attention for performance if you have a large dataset):
SELECT A.ACCOUNTKEY
, A.PARENTKEY
, (SELECT MIN(B.ACCOUNTKEY) FROM MYT B WHERE B.GROUPKEY IS NULL AND A.ACCOUNTKEY<=B.ACCOUNTKEY) AS NEW_PARID
FROM MYT A
/* WHERE A.GROUPKEY IS NOT NULL*/
This is the code I need to rewrite to work on SQL Server 2008
SELECT AccountKey,
LineName,
AccountName,
GroupKey,
AccountNumber,
ParentAccountKey
INTO tempAccount
FROM
(
SELECT AccountKey,
LineName,
AccountName,
GroupKey,
AccountNumber,
AccountKey AS ParentAccountKey,
LAG(AccountKey) OVER(ORDER BY AccountKey) AS PREC
FROM tempTable2
WHERE GroupKey IS NULL
UNION ALL
SELECT A.AccountKey,
A.LineName,
A.AccountName,
A.GroupKey,
A.AccountNumber,
B.AccountKey AS ParentAccountKey,
B.PREC
FROM tempTable2 A
INNER JOIN
(
SELECT AccountKey,
LineName,
AccountName,
GroupKey,
AccountNumber,
AccountKey AS ParentAccountKey,
LAG(AccountKey) OVER(ORDER BY AccountKey) AS PREC
FROM tempTable2
WHERE GroupKey IS NULL
) B ON A.AccountKey < B.AccountKey
AND (B.PREC IS NULL
OR B.PREC < A.AccountKey)
WHERE A.GroupKey IS NOT NULL
AND B.GroupKey IS NULL
) X
ORDER BY AccountKey;

sql join on Nearest greater than Value

If i have the following database structure:
tbl1
|id | EYearMonth |
| 1 | 1617 |
| 2 | 1618 |
| 3 | 1619 |
| 4 | 1620 |
| 5 | 1621 |
| 6 | 1622 |
| 7 | 1623 |
| 8 | 1624 |
| 9 | 1625 |
| 10 | 1626 |
| 11 | 1627 |
| 12 | 1628 |
Tbl2
|id | Value | Serial#
| 1 | 1617 | 1068
| 2 | 1618 | 1104
| 3 | 1624 | 1215
What I really want, is the following:
Result
|id | EYearMonth | Serial#
| 1 | 1617 | 1068
| 2 | 1618 | 1104
| 3 | 1619 | 1104
| 4 | 1620 | 1104
| 5 | 1621 | 1104
| 6 | 1622 | 1104
| 7 | 1623 | 1104
| 8 | 1624 | 1215
| 9 | 1625 | 1215
| 10 | 1626 | 1215
| 11 | 1627 | 1215
| 12 | 1628 | 1215
How can I make this Result? Please Help Me
You can use CROSS APPLY and TOP for this:
SELECT *
FROM tbl1 t1
CROSS APPLY(
SELECT TOP 1 t2.[Serial#]
FROM tbl2 t2
WHERE t2.Value <= t1.EYearMonth
ORDER BY t2.Value DESC
)t2
ONLINE DEMO
The following query will work:
SELECT
T1.id,
T1.EYearMonth,
T2.Serial#
FROM tbl1 T1
INNER JOIN Tbl2 T2
ON tbl1.EYearMonth = Tbl2.Value

Resources