我們可以做updat Ë但與ROW_NUMBER和MIN功能也select語句我在這裏展示一樣...
declare @t table (custid int,datet date,Amount int)
insert into @t(custid,datet,Amount)values (1,'20150301',10000)
insert into @t(custid,datet,Amount)values (1,'20150302',5000)
insert into @t(custid,datet,Amount)values (1,'20150303',15000)
insert into @t(custid,datet,Amount)values (2,'20150208',9000)
insert into @t(custid,datet,Amount)values (2,'20150201',5000)
insert into @t(custid,datet,Amount)values (2,'20150202',4000)
;with CTE AS
(
select *,ROW_NUMBER()OVER(PARTITION BY custid order by datet)RN from @t
)
Select custid,datet,case when RN = 1 then amount ELSE 0 END Amt from cte
OR
;with CTE AS
(
select *,min(datet)OVER(PARTITION BY custid)RN from @t
)
Select custid,datet,case when RN = MIN(datet) then amount ELSE 0 END Amt from cte
group by custid,datet,Amount,RN
您可以從第一個的子選擇刪除GROUP BY。 – jarlh 2015-03-19 12:34:00
@jarlh是的,當然,謝謝。我已經相應更新了。 – 2015-03-19 12:36:44