如何增加列对另一列值的关注

时间:2013-07-25 10:07:35

标签: sql sql-server auto-increment

我有以下数据:

(1,'20120615 8:03:43 PM'),
(1,'20120615 8:03:43 PM'),
(1,'20120615 8:03:48 PM'),
(1,'20120615 8:03:53 PM'),
(0,'20120615 8:03:58 PM'),
(1,'20120615 8:04:03 PM'),
(1,'20120615 8:04:08 PM'),
(1,'20120615 8:04:13 PM'),
(1,'20120615 8:04:18 PM'),
(0,'20120615 8:04:23 PM'),
(1,'20120615 8:04:28 PM'),
(1,'20120615 8:04:33 PM');

我想要的结果是:

(1,'20120615 8:03:43 PM', 1),
(1,'20120615 8:03:43 PM', 1),
(1,'20120615 8:03:48 PM', 1),
(1,'20120615 8:03:53 PM', 1),
(0,'20120615 8:03:58 PM', 0),
(1,'20120615 8:04:03 PM', 2),
(1,'20120615 8:04:08 PM', 2),
(1,'20120615 8:04:13 PM', 2),
(1,'20120615 8:04:18 PM', 2),
(0,'20120615 8:04:23 PM', 0),
(1,'20120615 8:04:28 PM', 3),
(1,'20120615 8:04:33 PM', 3);

换句话说,我想按照上面的顺序对0以外的数据进行分组。

4 个答案:

答案 0 :(得分:4)

这是个主意。为每个组的起始位置创建一个标志。这在您上面的数据中确实是1 - col1。然后取这个标志的累积总和。对于不是0的值,这是该组。以下查询采用此方法,使用相关子查询进行计算:

select t.col1, t.dt,
       (case when t.col1 = 0 then 0 else 1+grouping end)
from (select t.*,
             (select sum(1-col1)
              from t t2
              where t2.dt <= t.dt
             ) as grouping
      from t
     ) t;

在SQL Server 2012中,您可以使用累积总和来执行此操作:

select t.col1, t.dt,
       (case when col1 = 0 then 0
             else 1+sum(1 - col1) over (order by dt)
        end)
from t;

答案 1 :(得分:3)

试试这个 -

<强> DDL:

DECLARE @temp TABLE (Col1 INT, Col2 DATETIME)

INSERT INTO @temp (Col1, Col2)
VALUES 
     (1,'20120615 8:03:43 PM'),
     (1,'20120615 8:03:43 PM'),
     (1,'20120615 8:03:48 PM'),
     (1,'20120615 8:03:53 PM'),
     (0,'20120615 8:03:58 PM'),
     (1,'20120615 8:04:03 PM'),
     (1,'20120615 8:04:08 PM'),
     (1,'20120615 8:04:13 PM'),
     (1,'20120615 8:04:18 PM'),
     (0,'20120615 8:04:23 PM'),
     (1,'20120615 8:04:28 PM'),
     (1,'20120615 8:04:33 PM')

<强>查询:

;WITH cte AS 
(
     SELECT Col2, Col3 = ROW_NUMBER() OVER (ORDER BY Col2)
     FROM @temp t2
     WHERE t2.Col1 = 0
)
SELECT t.Col1, t.Col2, ISNULL(t2.Col3, 0) + t.Col1 
FROM @temp t
OUTER APPLY (
     SELECT TOP 1 Col2, Col3
     FROM cte t2
     WHERE t.Col2 > t2.Col2
          AND t.Col1 = 1
     ORDER BY t2.Col2 DESC
) t2

<强>输出:

Col1        Col2                    Col3
----------- ----------------------- --------------------
1           2012-06-15 20:03:43.000 1
1           2012-06-15 20:03:43.000 1
1           2012-06-15 20:03:48.000 1
1           2012-06-15 20:03:53.000 1
0           2012-06-15 20:03:58.000 0
1           2012-06-15 20:04:03.000 2
1           2012-06-15 20:04:08.000 2
1           2012-06-15 20:04:13.000 2
1           2012-06-15 20:04:18.000 2
0           2012-06-15 20:04:23.000 0
1           2012-06-15 20:04:28.000 3
1           2012-06-15 20:04:33.000 3

答案 2 :(得分:2)

相关子查询的另一个选项。而不是@Gordon Linoff建议的MAX函数,此查询使用COUNT函数

SELECT *, (
           SELECT CASE WHEN t1.IntCol = 0 THEN 0 ELSE COUNT(*) + 1 END
           FROM dbo.test139 t2
           WHERE t2.IntCol = 0
             AND t1.dateCol > t2.dateCol
           ) AS IntCol2
FROM dbo.test139 t1

请参阅SQLFiddle

上的演示

答案 3 :(得分:0)

请尝试此操作,您需要更改一些列名和表名

DECLARE @TABLE_COUNT BIGINT
DECLARE @LOOP_COUNT BIGINT=0
DECLARE @COULMN_A INT
DECLARE @COULMN_B DATETIME
DECLARE @COUNTER BIGINT=1
DECLARE @NEWTABLE TABLE(COULMN_A INT,COLUMN_B DATETIME,COUNTER INT)
SELECT @TABLE_COUNT= COUNT(*) FROM TABLE

WHILE @LOOP_COUNT<@TABLE_COUNT
BEGIN
SELECT @COULMN_A=COLUMN_A,@COULMN_B=COLUMN_B FROM TABLE
IF @COULMN_A=0
SELECT @COUNTER=@COUNTER+1

INSERT INTO @NEWTABLE VALUES(@COULMN_A,@COULMN_B,@COUNTER)

SELECT @LOOP_COUNT=@LOOP_COUNT+1
END

SELECT * FROM @NEWTABLE

如果需要任何理解方面的帮助,请麻烦我

希望这有效

此致 Ashutosh Arya