dongwu8050 2012-03-18 14:58
浏览 34
已采纳

获取行之间具有固定值差异的行并减少连续日期

Structure of my table : ID(int) | NUMBER(int) | CREATED_AT(date)

If I have three numbers : 10, 11 and 9, Can I get all rows from my table with same ratio of NUMBER values with decreasing consecutive CREATED_AT dates?

Examples :

If my numbers are : 10,11,9 ... The rows may be :

NUMBER | CREATED_AT
50     | 2012-03-18
51     | 2012-03-17
49     | 2012-03-16

Result must be "50"

If my numbers are : 50,40,60 ... The rows may be :

NUMBER | CREATED_AT
100    | 2012-02-20
90     | 2012-02-19
110    | 2012-02-18

Result must be "100"

...

I wasted a half of day for this, but I still have no Idea where to start...

P.S : I have about 5 000 rows in my table.

UPDATE

I did it with a bit of php code and a lot of mysql queries. Script execution time : ~6 seconds. [Tested on godaddy deluxe hosting] =)

  • 写回答

2条回答 默认 最新

  • dooid3005 2012-03-18 15:51
    关注

    I haven't tested it myself, but you can try this:

    SELECT a.*,b.*,c.*
    FROM table_name a
    INNER JOIN table_name b 
    ON b.NUMBER = a.NUMBER + $range1 and DATEDIFF(b.CREATED_AT, a.CREATED_AT) = 1
    INNER JOIN table_name c 
    ON c.NUMBER = b.NUMBER + $range2 and DATEDIFF(c.CREATED_AT, b.CREATED_AT) = 1
    

    $range1, $range2 are variables number1 - number2 and number2 - number3.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大