mysql昨天和今天的值之间的差异

dvtswwa3  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(625)

我知道,这应该很简单,但我似乎找不到解决办法或创造一个。
我只想数一数日期之间的差异。
所以呢
5/2和5/01=4
5/1和4/30=5
等。

这是我目前得到的结果,但我想知道是否因为我的一些行中没有值,所以会把事情搞砸。。。。

SELECT g1.customer_count, (g2.customer_count - g1.customer_count) as DiffDaily 
from trux_customer_site_service_counts_max_df g1 
    inner join trux_customer_site_service_counts_max_df g2 
        on g2.Row_Number = g1.Row_Number +1 
where g1.customer_count is not null
oxiaedzo

oxiaedzo1#

感谢安基特,是他让我踏上了旅途。因为我在domomysql工作,所以你必须做一些不同的事情

SELECT 

trux_customer_site_service_counts_max_df.*
, @rownumber :=@rownumber +1 as RowNumber

from trux_customer_site_service_counts_max_df,
(select @rownumber :=0) as t
where customer_count is not null

第2步

SELECT
g1.*,

(g2.customer_count - g1.customer_count) as DiffDaily

from customercount_no_nulls g1
inner join customercount_no_nulls g2  on g2.RowNumber  = g1.RowNumber +1
tkqqtvp1

tkqqtvp12#

您应该进行内部连接,如下图所示:

SELECT 
  A.DATE_OF_DATA `DD-1`, 
  DATE(B.DATE_OF_DATA) DD,
 (B.CUSTOMER_COUNT - A.CUSTOMER_COUNT) CUSTOMER_COUNT_DIFF
FROM 
 YOUR_TABLE A INNER JOIN YOUR_TABLE B
ON DATE(A.DATE_OF_DATA)=DATE(B.DATE_OF_DATA - INTERVAL 1 DAY);

相关问题