環境:MS SERVER 2022
每天每一本書賣出的數量
如果想看今天賣出數量&昨天賣出數量,怎麼做呢?
create table #sell
(
date int
,bookid int
,qty int
)
insert into #sell select 20240101,1,10
insert into #sell select 20240101,2,20
insert into #sell select 20240102,1,100
insert into #sell select 20240102,2,200
insert into #sell select 20240104,1,111
insert into #sell select 20240104,2,222
select * ,lag(qty) over (partition by bookid order by date)
from #sell
結果呈現