2

我有一张看起来像的桌子:

INDEX       IsValid                StopPoint              TripSubId
----------- ---------------------- ---------------------- -----------
7           7                      NULL                   NULL
9           9                      NULL                   NULL
10          10                     NULL                   NULL
13          13                     13                     NULL
18          18                     NULL                   NULL
25          25                     NULL                   NULL
26          26                     NULL                   NULL
27          27                     NULL                   NULL
28          28                     NULL                   NULL
29          29                     29                     NULL
30          30                     NULL                   NULL
31          31                     NULL                   NULL
32          32                     NULL                   NULL
33          33                     NULL                   NULL
34          34                     34                     NULL
35          35                     NULL                   NULL
36          36                     NULL                   NULL
37          37                     NULL                   NULL
39          39                     NULL                   NULL
40          40                     NULL                   NULL

我想像这样更新最后一列:

INDEX       IsValid                StopPoint              TripSubId
----------- ---------------------- ---------------------- -----------
7           7                      NULL                    1
9           9                      NULL                    1
10          10                     NULL                    1
13          13                     13                      1
18          18                     NULL                    2
25          25                     NULL                    2
26          26                     NULL                    2
27          27                     NULL                    2
28          28                     NULL                    2
29          29                     29                      2 
30          30                     NULL                    3
31          31                     NULL                    3
32          32                     NULL                    3
33          33                     NULL                    3
34          34                     34                      3
35          35                     NULL                    4
36          36                     NULL                    4
37          37                     NULL                    4
39          39                     NULL                    4
40          40                     NULL                    4

规则是,一旦我达到 column 上不为空的点,我希望数字增加StopPoint

我该怎么做?

4

6 回答 6

2
WITH T AS
(
SELECT 
    *,
    COUNT(StopPoint) OVER (ORDER BY [INDEX] ROWS UNBOUNDED PRECEDING) AS Cnt
FROM YourTable
)
UPDATE T 
SET TripSubId = Cnt + CASE WHEN StopPoint IS NULL THEN 1 ELSE 0 END

SQL小提琴

于 2013-07-23T14:18:02.263 回答
1
SELECT 
 [INDEX] , [IsValid], [StopPoint]
, [TripSubId] = 
    (SELECT COUNT(*)  + 1 
     FROM tbl t2 
     WHERE t2.[INDEX] <=  t.[INDEX] AND StopPoint IS NOT NULL)
FROM tbl t    

SQL小提琴

于 2013-07-23T13:42:19.287 回答
1

您真的需要将列存储在数据库中,或者您可以在选择时计算它吗?

SELECT o.INDEX, o.IsValid, o.StopPoint,
    ( SELECT COUNT(*) FROM myTable i where i.INDEX <= o.INDEX AND i.StopPoint IS NOT NULL ) as TripSubID
FROM myTable o
于 2013-07-23T13:37:25.040 回答
1

这是你的数据

声明@tmpTable 表(Idx int、IsValid int、StopPoint int、TripSubID int

)

INSERT @tmpTable (Idx,IsValid,StopPoint,TripSubId)
SELECT 7,7,NULL,NULL union
SELECT 9,9,NULL,NULL union
SELECT 10,10,NULL,NULL union
SELECT 13,13,13,NULL union
SELECT 18,18,NULL,NULL union
SELECT 25,25,NULL,NULL union
SELECT 26,26,NULL,NULL union
SELECT 27,27,NULL,NULL union
SELECT 28,28,NULL,NULL union
SELECT 29,29,29,NULL union
SELECT 30,30,NULL,NULL union
SELECT 31,31,NULL,NULL union
SELECT 32,32,NULL,NULL union
SELECT 33,33,NULL,NULL union
SELECT 34,34,34,NULL union
SELECT 35,35,NULL,NULL union
SELECT 36,36,NULL,NULL union
SELECT 37,37,NULL,NULL union
SELECT 39,39,NULL,NULL union
SELECT 40,40,NULL,NULL;

和你的更新

with cte as (
    select StopPoint,ROW_NUMBER() OVER (ORDER BY StopPoint) TripSubID
    from @tmpTable
    where StopPoint is not null
)

update @tmpTable
set TripSubID=(select MIN(TripSubID) from cte where a.Idx<=cte.StopPoint)
from @tmpTable a
于 2013-07-23T14:04:27.657 回答
1
update temp set
    TripSubId = C.cnt
from temp as T
    outer apply (select count(TT.StopPoint) + 1 as cnt from temp as TT where TT.INDEX < T.INDEX) as C
于 2013-07-23T13:37:00.237 回答
0
DECLARE @t table (
   indx      int
 , isvalid   int
 , stoppoint int
 , tripsubid int
);

INSERT INTO @t (indx, isvalid, stoppoint, tripsubid)
  VALUES ( 7,  7, NULL, NULL)
       , ( 9,  9, NULL, NULL)
       , (10, 10, NULL, NULL)
       , (13, 13, 13  , NULL)
       , (18, 18, NULL, NULL)
       , (25, 25, NULL, NULL)
       , (26, 26, NULL, NULL)
       , (27, 27, NULL, NULL)
       , (28, 28, NULL, NULL)
       , (29, 29, 29  , NULL)
       , (30, 30, NULL, NULL)
       , (31, 31, NULL, NULL)
       , (32, 32, NULL, NULL)
       , (33, 33, NULL, NULL)
       , (34, 34, 34  , NULL)
       , (35, 35, NULL, NULL)
       , (36, 36, NULL, NULL)
       , (37, 37, NULL, NULL)
       , (39, 39, NULL, NULL)
       , (40, 40, NULL, NULL);

SELECT t.indx
     , t.isvalid
     , t.stoppoint
     , Dense_Rank() OVER (ORDER BY y.stoppoint)
FROM   @t As t
 CROSS
 APPLY (
        SELECT Coalesce(Max(stoppoint), -1) As stoppoint
        FROM   @t
        WHERE  stoppoint IS NOT NULL
        AND    indx < t.indx
       ) As y
ORDER
    BY t.indx;
于 2013-07-23T13:47:48.030 回答