Related
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 |
+----------+---------+--------+--------+---------------+------------+------------+
We are trying to get a combined table where we also try to sum the volume.
Dateset right now:
+-------------+-----+------------+------------+--------+---------+
| Voorziening | BSN | Begindatum | Einddatum | Volume | Product |
+-------------+-----+------------+------------+--------+---------+
| 1000 | 1 | 1-1-2017 | 31-1-2017 | 50 | AAAA |
+-------------+-----+------------+------------+--------+---------+
| 1200 | 1 | 1-2-2017 | 31-3-2017 | 200 | AAAA |
+-------------+-----+------------+------------+--------+---------+
| 1250 | 1 | 1-4-2017 | 10-4-2017 | 90 | AAAA |
+-------------+-----+------------+------------+--------+---------+
| 1111 | 2 | 4-1-2017 | 10-1-2017 | 4 | AABB |
+-------------+-----+------------+------------+--------+---------+
| 1345 | 2 | 11-1-2017 | 29-1-2017 | 80 | AABB |
+-------------+-----+------------+------------+--------+---------+
| 2000 | 1 | 10-1-2017 | 31-1-2017 | 90 | CCCC |
+-------------+-----+------------+------------+--------+---------+
| 2190 | 1 | 1-2-2017 | 31-12-2017 | 100 | CCCC |
+-------------+-----+------------+------------+--------+---------+
What I want to achieve
+-------------+-----+------------+------------+--------+---------+
| Voorziening | BSN | Begindatum | Einddatum | Volume | Product |
+-------------+-----+------------+------------+--------+---------+
| 1000 | 1 | 1-1-2017 | 10-4-2017 | 340 | AAAA |
+-------------+-----+------------+------------+--------+---------+
| 2000 | 1 | 10-1-2017 | 31-12-2017 | 190 | CCCC |
+-------------+-----+------------+------------+--------+---------+
| 1111 | 2 | 4-1-2017 | 29-1-2017 | 84 | AABB |
+-------------+-----+------------+------------+--------+---------+
What i've got so for is the folowwing query:
SELECT min(b.Voorziening) as voorzieningsnummer
,a.BSN
,min(b.Begindatum) as mindatum
,MAX(b.Einddatum) AS maxdatum
,a.Productcode
,
(SELECT sum(Volume)
FROM Voorziening
)as totaal
FROM Voorziening a
INNER JOIN Voorziening b
ON a.BSN = b.BSN
AND a.Productcode = b.Productcode
GROUP BY a.BSN, a.Productcode
The result is gives me is this:
+-------------+-----+------------+------------+--------+
| Voorziening | BSN | Begindatum | Einddatum | Volume |
+-------------+-----+------------+------------+--------+
| 1000 | 1 | 1-1-2017 | 10-4-2017 | 424 |
+-------------+-----+------------+------------+--------+
| 1111 | 2 | 4-1-2017 | 29-1-2017 | 424 |
+-------------+-----+------------+------------+--------+
You guys can help me to get the sum right?
There isn't any reason to use JOIN. you can use aggregate function directly.
You can try this.
SELECT min(a.Voorziening) as voorzieningsnummer
,a.BSN
,min(a.Begindatum) as mindatum
,MAX(a.Einddatum) AS maxdatum
,a.Productcode
,SUM(a.Volume) Volume
FROM Voorziening a
GROUP BY a.BSN, a.Productcode
if you are using sql server 2008 or above version then just go ahead with PARTITION BY
SUM(Volume)over(Partition by Product order by Voorziening,another,another)
I need to extract consecutive dates of a particular installation and reject all installations that does not have consecutive dates.
for example
The following is my query:
SELECT
Installation_no
,RDNG_STTS_CD
,UPDTD_DT
,ROW_NUMBER() OVER (PARTITION BY Installation_no ORDER BY UPDTD_DT ASC) AS RowNum
FROM
mrdg_tbl_download_DailyBKP
WHERE
RDNG_STTS_CD = 'PL';
that resulted in the following data:
+-----------------+--------------+-------------------------+--------+
| Installation_no | RDNG_STTS_CD | UPDTD_DT | RowNum |
+-----------------+--------------+-------------------------+--------+
| 5000002099 | PL | 2018-05-14 11:47:01.050 | 1 |
| 5000002099 | PL | 2018-05-14 11:47:01.050 | 2 |
| 5000002101 | PL | 2018-06-19 14:39:33.633 | 1 |
| 5000002101 | PL | 2018-06-19 14:39:33.633 | 2 |
| 5000002101 | PL | 2018-07-19 16:07:23.723 | 3 |
| 5000002101 | PL | 2018-07-19 16:07:23.723 | 4 |
| 5000002110 | PL | 2018-01-04 15:49:08.017 | 1 |
| 5000002110 | PL | 2018-01-04 15:49:08.017 | 2 |
| 5000002187 | PL | 2018-01-23 14:51:59.160 | 1 |
| 5000002187 | PL | 2018-01-23 14:51:59.160 | 2 |
| 5000002187 | PL | 2018-03-25 12:39:43.343 | 3 |
| 5000002187 | PL | 2018-03-25 12:39:43.343 | 4 |
| 5000002187 | PL | 2018-04-27 16:31:20.420 | 5 |
| 5000002187 | PL | 2018-04-27 16:31:20.420 | 6 |
| 5000002187 | PL | 2018-05-29 17:54:20.520 | 7 |
| 5000002187 | PL | 2018-05-29 17:54:20.520 | 8 |
| 5000002187 | PL | 2018-06-28 17:10:12.613 | 9 |
| 5000002187 | PL | 2018-06-28 17:10:12.613 | 10 |
| 5000002438 | PL | 2018-01-31 16:03:38.137 | 1 |
| 5000002438 | PL | 2018-01-31 16:03:38.137 | 2 |
| 5000002438 | PL | 2018-03-04 15:19:41.340 | 3 |
| 5000002438 | PL | 2018-03-04 15:19:41.340 | 4 |
| 5000002438 | PL | 2018-04-04 16:45:01.040 | 5 |
| 5000002438 | PL | 2018-04-04 16:45:01.040 | 6 |
| 5000002438 | PL | 2018-05-07 15:58:54.553 | 7 |
| 5000002438 | PL | 2018-05-07 15:58:54.553 | 8 |
| 5000002438 | PL | 2018-06-07 14:26:45.647 | 9 |
| 5000002438 | PL | 2018-06-07 14:26:45.647 | 10 |
| 5000002438 | PL | 2018-07-10 15:53:54.753 | 11 |
| 5000002438 | PL | 2018-07-10 15:53:54.753 | 12 |
+-----------------+--------------+-------------------------+--------+
The above is a sample data. and i have around 200 000 records in each category (here category is 'PL')
I need a distinct installation having consecutive month range (month 1,2,3,4,5).
I have tried every example in stack overflow but could not get precise answer.
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 |
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;