select mnth,qty ,sign(s1.qty-(select top 1 qty from sales as s2
where s2.mnth < s1.mnth
order by s2.mnth desc)) as sgn
from sales as s1
s2.mnth < s1.mnth好迷糊
哪位帮我解释一下同一表做比较
分不多
...全文
986打赏收藏
同一表对比
select mnth,qty ,sign(s1.qty-(select top 1 qty from sales as s2 where s2.mnth < s1.mnth order by s2.mnth desc)) as sgn from sales as s1 s2.mnth < s1.mnth好迷糊 哪位帮我解释一下同一表做比较 分不多
(select top 1 qty from sales as s2
where s2.mnth < s1.mnth
order by s2.mnth desc)
这个表示查询S1离目前记录 最近的一条之前的记录,比如说当S1的是1月31号,qty是500,
那么子查询查出离1月31号最近的之前的记录,再相减,判断正负,以此判断是qty随 mnth的变化增加还是减少了。
select mnth,qty ,sign
(
s1.qty-
(
select top 1 qty
from sales as s2 --就当另外一张表看就好了
where s2.mnth < s1.mnth
order by s2.mnth desc
)
) as sgn
from sales as s1
lz 你的格式太不清晰了。
select mnth,qty ,sign
(
s1.qty-
(
select top 1 qty
from sales as s2 --就当另外一张表看就好了
where s2.mnth < s1.mnth
order by s2.mnth desc
)
) as sgn
from sales as s1