如果值为 -1,则返回上一条记录的值

发布于 2024-10-20 01:41:38 字数 576 浏览 2 评论 0原文

我有一个包含 LocationId 字段的表。在某些情况下,如果记录共享相同的外键,则 LocationId 可能会显示为 -1。

我想要做的是在发生这种情况的情况下,在我的选择查询中查找之前的位置。

示例数据:

Record  FK     StartTime               EndTime          Location
1       110  2011/01/01 12.30        2011/01/01 6.10      456
2       110  2011/01/01 3.40         2011/01/01 4.00       -1
3       110  2011/01/02 1.00         2011/01/02 8.00      891
4       110  2011/01/02 5.00         2011/01/02 6.00       -1
5       110  2011/01/02 6.10         2011/01/02 6.30       -1

记录 2 的 -1 应为 456,记录 4 和 5 的 -1 应为 891

I've got a table that contains a LocationId field. In some cases, where a record shares the same foreign key, the LocationId might come through as -1.

What I want to do is in my select query is in the case of this happening, the previous location.

Example data:

Record  FK     StartTime               EndTime          Location
1       110  2011/01/01 12.30        2011/01/01 6.10      456
2       110  2011/01/01 3.40         2011/01/01 4.00       -1
3       110  2011/01/02 1.00         2011/01/02 8.00      891
4       110  2011/01/02 5.00         2011/01/02 6.00       -1
5       110  2011/01/02 6.10         2011/01/02 6.30       -1

The -1 should come out as 456 for record 2, and 891 for record 4 and 5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星星的轨迹 2024-10-27 01:41:38

对于整个结果集

with tmp(Record ,FK ,StartTime ,EndTime ,Location)
as( select
1, 110 ,'2011/01/01 12:30', '2011/01/01 6:10', 456 union all select
2, 110 ,'2011/01/01 3:40', '2011/01/01 4:00', -1 union all select
3, 110 ,'2011/01/02 1:00', '2011/01/02 8:00', 891 union all select
4, 110 ,'2011/01/02 5:00', '2011/01/02 6:00', -1 union all select
5, 110 ,'2011/01/02 6:10', '2011/01/02 6:30', -1
)

-- ignore above this line
select curr.Record, curr.FK, curr.StartTime, curr.EndTime,
 case when curr.Location=-1 then prev.Location else curr.Location end Location
from tmp curr
outer apply 
 (select top 1 prev.location
 from tmp prev
 where curr.location=-1 and prev.FK=curr.FK
 and prev.starttime < curr.starttime
 and prev.location <> -1
 order by prev.starttime desc) prev

For the entire result set

with tmp(Record ,FK ,StartTime ,EndTime ,Location)
as( select
1, 110 ,'2011/01/01 12:30', '2011/01/01 6:10', 456 union all select
2, 110 ,'2011/01/01 3:40', '2011/01/01 4:00', -1 union all select
3, 110 ,'2011/01/02 1:00', '2011/01/02 8:00', 891 union all select
4, 110 ,'2011/01/02 5:00', '2011/01/02 6:00', -1 union all select
5, 110 ,'2011/01/02 6:10', '2011/01/02 6:30', -1
)

-- ignore above this line
select curr.Record, curr.FK, curr.StartTime, curr.EndTime,
 case when curr.Location=-1 then prev.Location else curr.Location end Location
from tmp curr
outer apply 
 (select top 1 prev.location
 from tmp prev
 where curr.location=-1 and prev.FK=curr.FK
 and prev.starttime < curr.starttime
 and prev.location <> -1
 order by prev.starttime desc) prev
醉生梦死 2024-10-27 01:41:38

您可以使用相关子查询。例如:

SELECT *
    , (SELECT TOP 1 Location
       FROM MyTable T2
       WHERE T2.Record <= T1.Record
       AND T2.FK = T1.FK
       AND T2.Location <> -1
       ORDER BY T2.Record DESC) AS BestLocation
FROM MyTable T1

You can use a correlated subquery. For example:

SELECT *
    , (SELECT TOP 1 Location
       FROM MyTable T2
       WHERE T2.Record <= T1.Record
       AND T2.FK = T1.FK
       AND T2.Location <> -1
       ORDER BY T2.Record DESC) AS BestLocation
FROM MyTable T1
撧情箌佬 2024-10-27 01:41:38
SELECT
    Record,
    FK,
    StartTime,
    EndTime,
(
    SELECT
        Location
    FROM
        MyTable
    WHERE
        Record =
    ( 
        SELECT
            MAX(Record)
        FROM
            MyTable t2
        WHERE
            t2.Record =< t1.Record AND
            Location >= 0
    )
) 

FROM
    MyTable t1
SELECT
    Record,
    FK,
    StartTime,
    EndTime,
(
    SELECT
        Location
    FROM
        MyTable
    WHERE
        Record =
    ( 
        SELECT
            MAX(Record)
        FROM
            MyTable t2
        WHERE
            t2.Record =< t1.Record AND
            Location >= 0
    )
) 

FROM
    MyTable t1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文