有DDL
CREATE TABLE #ServiceChange(
[ID] [int] identity(1,1),
[comp] [char](2) NOT NULL,
[date] [numeric](8, 0) NOT NULL,
[custid] [numeric](7, 0) NOT NULL,
[service] [char](35) NOT NULL,
[type] [char](1) NOT NULL,
[amount] [numeric](9, 2) NOT NULL,
[custname] [char](30) NOT NULL,
[GROUPID] [char](2) NULL
)
以下链接中的原始和欲望数据
https://www.dropbox.com/sh/bpapxquaae9aa13/AADnan31ZASublDjN7sa2Vvza
我想知道如何使用SQL Server 2012根据某些条件修改当前和下一条记录的一列?
标准是:
Type should always flow F->T
if current abs(amount)> next abs(amount) then groupid = 'PD'
if current abs(amount)< next abs(amount) then groupid = 'PI'
there is no case when those amounts will be equals
where current(custid) = next(custid) and current(service) = next(service) and groupid is null
任何帮助都将非常感激。
谢谢
答案 0 :(得分:1)
未找到Custid,服务和金额。没有下一个的定义。
这假设接下来基于ID
update table
set t1.groupid = 'PD', t1.type = 'f', t2.type = 't'
from table t1
join table t2
on t2.id = t1.id + 1
and abs(t1.amount) > abs(t2.amount)
and t1.groupid is null
and t1.cusid = t2.custid
and t2.service = t2.service
update table
set t1.groupid = 'PI'
from table t1
join table t2
on t2.id = t1.id + 1
and abs(t1.amount) < abs(t2.amount)
...