|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 gpk 于 2015-7-27 22:14 编辑
版主的代码:
select t1.id,t1.name,t1.ymd,t1.today,t2.PRICE as yesterday,today-yesterday as [+-] from
(select A.id,A.NAME,A.YMD,A.PRICE AS today,MAX(B.YMD) AS BYMD FROM [ovrn$]A
LEFT JOIN
[ovrn$]B
ON A.NAME=B.NAME AND A.YMD>B.YMD
GROUP BY A.id,A.NAME,A.YMD,A.PRICE)t1
left join
[ovrn$]t2
on t1.name=t2.name and t1.bymd=t2.ymd
你可以把语句拆分开了,版主语句外层是t1 left join t2,
t1的语句是:
select A.id,A.NAME,A.YMD,A.PRICE AS today,MAX(B.YMD) AS BYMD FROM [ovrn$]A
LEFT JOIN
[ovrn$]B
ON A.NAME=B.NAME AND A.YMD>B.YMD
GROUP BY A.id,A.NAME,A.YMD,A.PRICE
你新建一个sheet1,用上面t1的语句生成表,然后在边上用下面的语句,对比下就知道了:
select A.id,A.NAME,A.YMD,A.PRICE AS today,MAX(B.YMD) AS BYMD FROM [ovrn$]A
LEFT JOIN
[ovrn$]B
ON A.NAME=B.NAME AND B.YMD>A.YMD
GROUP BY A.id,A.NAME,A.YMD,A.PRICE
版主的自动结转库存语句我就是这样将过渡表一个个做出来才理解的。
供参考
ps:max是最大值,last是表中最后一行值,first是表中第一行的值,虽然某些情况下等同,但是根据数据的规律,max通用性更好,如果非要用last的话,就得保证源数据按日期顺序排序的,这样last返回的结果等同于max返回的结果。
|
评分
-
1
查看全部评分
-
|