【问题标题】:Update Statement for Last Card Status最后一张卡状态的更新声明
【发布时间】:2016-06-12 01:20:30
【问题描述】:

我有一张卡片状态为:1(打开)

我有另一张卡片,卡片状态为:2(已关闭)

我运行的工作使两张卡都处于 cardstatus: 3 (Hold)

现在,我想编写一个更新语句,它将两张卡都设置为以前的状态。我只是给你 1 和 1 卡作为例子。

实际上有 1000 多张卡片,显然我不想编写那么多更新语句,也不想对当前代码进行太多更改。目前,update 语句中的逻辑将所有保留卡(cardstatus 3)置于活动状态(cardstatus 1)。

卡片表有卡片状态(当前状态)

cardaudit 表有 cardstatus(以前的卡状态)

cardaudit 表中的 max(cardauditid) 始终是 laststatus --- 这是我想要的,而不是所有卡都处于活动状态

card 和 cardaudit 表有共同的 cardstatus

我正在写:

update card set cardstatus = (some query to get the previous status)

更新!!!

我得到了答案。

【问题讨论】:

  • 样本数据和期望的结果将真正阐明您想要做什么。
  • 请看上面....我提供了示例数据。

标签: sql oracle sql-update dml


【解决方案1】:

使用样本数据会更容易,但仍然:

create table credit (tid int, stat varchar(10))

insert into credit values (1,'hold'),(2,'hold')

create table creditaudit (id int identity, tid int, stat varchar(10))

insert into creditaudit (tid, stat) 
values (1,'open'), (1,'close'), (2,'open'), (1,'active'), (2,'close')

select * from  credit
select * from  creditaudit

;with cte 
as (select tid, stat, row_number() over(partition by tid order by id desc) rn
    from creditaudit)
select * from credit
    join cte on credit.tid = cte.tid
where rn = 1

【讨论】:

    猜你喜欢
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多