我有一张表,有Intime,OutTime,zonechk标志
如果1表示内部,null表示外部,我需要合并它们以报告例如
里面10:10到10:40
里面11:10到12:30
12:31至14:00之外
14:10到14:40
在15:10到15:30之间
是
里面10:10到12:30
12:31至14:00之外
14:10到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 |
+--------+--------------+----------------+--------------+----------+-------------------------+--------+---------+
如何在TSQL中使用滞后删除?滞后必须是数字,zoneId是inTime ASC
答案 0 :(得分:1)
使用cross apply
可能会有所帮助,例如
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')
;
查询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 |