【问题标题】:Add top row value to current row based on condition根据条件将顶行值添加到当前行
【发布时间】:2013-05-03 14:46:42
【问题描述】:

我当前的表格输出是

        ---------------------------------------------------
        |    id               col1            col2        |
        ---------------------------------------------------
        |    1           |    test1      |    1           |
        |    2           |    test11     |    0           |
        |    3           |    test12     |    0           |
        |    4           |    test13     |    0           |
        |    5           |    test14     |    0           |
        |    6           |    test2      |    2           |
        |    7           |    test21     |    0           |
        |    8           |    test22     |    0           |
        |    9           |    test23     |    0           |
        |    10          |    test24     |    0           |
        ---------------------------------------------------

预期输出是

        ---------------------------------------------------
        |    id               col1            col2        |
        ---------------------------------------------------
        |    1           |    test1      |    1           |
        |    2           |    test11     |    1           |
        |    3           |    test12     |    1           |
        |    4           |    test13     |    1           |
        |    5           |    test14     |    1           |
        |    6           |    test2      |    2           |
        |    7           |    test21     |    2           |
        |    8           |    test22     |    2           |
        |    9           |    test23     |    2           |
        |    10          |    test24     |    2           |
        ---------------------------------------------------

没有光标这可能吗?有没有一种方法可以在当前行值为 0 的条件下将顶行值添加到当前行值?

【问题讨论】:

  • 什么版本的 SQL Server?以后的版本有更好的答案。
  • SQL server 2008。让我知道 2012 是否有更好的方法来处理这个问题。我可以搬到 2012 年。

标签: sql sql-server sql-server-2008-r2 sql-server-2012


【解决方案1】:

您可以找到col2 的最后一个非零值,例如:

select  id
,       col1
,       (
        select  top 1 col2
        from    YourTable yt2
        where   yt2.id <= yt1.id
                and yt2.col2 <> 0
        order by
                yt2.id desc
        )
from    YourTable yt1

Example at SQL Fiddle.

【讨论】:

  • 感谢您,这对您有很大帮助。这在 2012 年有效,但在 2008 年无效。如果我在 2008 年有选择,请告诉我。如果没有,我将搬到 2012 年。
  • 重新阅读您的帖子后,我认为您不是在寻找运行总和,而只是在寻找最后一个非零值。更新后的答案应该可以在 SQL Server 2008 中使用。
  • 我不认为我要添加的答案会比这个答案已经添加的更多,但为了成为社区的有用成员,我对标准的看法是: sqlfiddle.com/#!3/deaea/1
【解决方案2】:

看起来你不是在追求总和。这应该在 2005 年或以后工作:

DECLARE @tmp TABLE
    (
        id INT PRIMARY KEY
        , col1 VARCHAR(20)
        , col2 INT
    );
INSERT @tmp
VALUES
(1, 'test1',  1)
, (2, 'test11', 0)
, (3, 'test12', 0)
, (4, 'test13', 0)
, (5, 'test14', 0)
, (6, 'test2', 2)
, (7, 'test21', 0)
, (8, 'test22', 0)
, (9, 'test23', 0)
, (10, 'test24', 0);


SELECT 
    t1.id
    , t1.col1
    , CASE t1.col2
        WHEN 0 
            THEN t2.col2
        ELSE 
            t1.col2
    END col2
FROM 
    @tmp t1
OUTER APPLY
    (
        SELECT 
            TOP 1 col2
        FROM 
            @tmp t3
        WHERE 
            t3.id <= t1.id
            AND 
            t3.col2 > 0
        ORDER BY 
            t3.id DESC
    ) t2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    相关资源
    最近更新 更多