<bdo id='0EzCP'></bdo><ul id='0EzCP'></ul>

<tfoot id='0EzCP'></tfoot>

    <i id='0EzCP'><tr id='0EzCP'><dt id='0EzCP'><q id='0EzCP'><span id='0EzCP'><b id='0EzCP'><form id='0EzCP'><ins id='0EzCP'></ins><ul id='0EzCP'></ul><sub id='0EzCP'></sub></form><legend id='0EzCP'></legend><bdo id='0EzCP'><pre id='0EzCP'><center id='0EzCP'></center></pre></bdo></b><th id='0EzCP'></th></span></q></dt></tr></i><div id='0EzCP'><tfoot id='0EzCP'></tfoot><dl id='0EzCP'><fieldset id='0EzCP'></fieldset></dl></div>
  • <legend id='0EzCP'><style id='0EzCP'><dir id='0EzCP'><q id='0EzCP'></q></dir></style></legend>
  • <small id='0EzCP'></small><noframes id='0EzCP'>

        在SQL中从另一行数据中减去一行数据

        Subtracting one row of data from another in SQL(在SQL中从另一行数据中减去一行数据)

        <legend id='loRMm'><style id='loRMm'><dir id='loRMm'><q id='loRMm'></q></dir></style></legend>
        • <bdo id='loRMm'></bdo><ul id='loRMm'></ul>
            <tbody id='loRMm'></tbody>
          <i id='loRMm'><tr id='loRMm'><dt id='loRMm'><q id='loRMm'><span id='loRMm'><b id='loRMm'><form id='loRMm'><ins id='loRMm'></ins><ul id='loRMm'></ul><sub id='loRMm'></sub></form><legend id='loRMm'></legend><bdo id='loRMm'><pre id='loRMm'><center id='loRMm'></center></pre></bdo></b><th id='loRMm'></th></span></q></dt></tr></i><div id='loRMm'><tfoot id='loRMm'></tfoot><dl id='loRMm'><fieldset id='loRMm'></fieldset></dl></div>

              <tfoot id='loRMm'></tfoot>

                • <small id='loRMm'></small><noframes id='loRMm'>

                • 本文介绍了在SQL中从另一行数据中减去一行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我被一些 SQL 难住了,其中我有几行数据,我想从前一行中减去一行并让它一直重复.

                  这是表:

                  创建表 foo (ID,长度)

                  INSERT INTO foo (id,length) VALUES(1,1090)INSERT INTO foo (id,length) VALUES(2,888)INSERT INTO foo (id,length) VALUES(3,545)INSERT INTO foo (id,length) VALUES(4,434)插入 foo (id,length) VALUES(5,45)

                  我希望结果显示名为差异的第三列,即从下面一行中减去一行,最后一行从零中减去.

                  <前>+------+------------------------+|id |长度 |区别|+------+------------------------+|1 |1090 |202 ||2 |第888话第343话|3 |第545话111 ||4 |第434话第389话|5 |45 |45 |

                  我已经尝试过自连接,但我不确定如何限制结果而不是让它自行循环.对于给定的结果集,我不能确定 id 值是连续的,所以我没有使用该值.我可以扩展架构以包含某种顺序值.

                  这是我试过的:

                  SELECT id, f.length, f2.length, (f.length - f2.length) AS 差异FROM foo f, foo f2

                  感谢您的帮助.

                  解决方案

                  这可能对您有所帮助.

                  <前><代码>选择a.id,a.length,合并(a.length -(select b.length from foo b where b.id = a.id + 1), a.length) 作为差异来自 foo a

                  I've been stumped with some SQL where I've got several rows of data, and I want to subtract a row from the previous row and have it repeat all the way down.

                  So here is the table:

                  CREATE TABLE foo (
                    id,
                    length
                  )

                  INSERT INTO foo (id,length) VALUES(1,1090)
                  INSERT INTO foo (id,length) VALUES(2,888)
                  INSERT INTO foo (id,length) VALUES(3,545)
                  INSERT INTO foo (id,length) VALUES(4,434)
                  INSERT INTO foo (id,length) VALUES(5,45)

                  I want the results to show a third column called difference which is one row subtracting from the one below with the final row subtracting from zero.

                  +------+------------------------+
                  | id   |length |  difference  |
                  +------+------------------------+
                  |    1 | 1090  |  202         |
                  |    2 |  888  |  343         |
                  |    3 |  545  |  111         |
                  |    4 |  434  |  389         |
                  |    5 |   45  |   45         |

                  I've tried a self join but I'm not exactly sure how to limit the results instead of having it cycle through itself. I can't depend that the id value will be sequential for a given result set so I'm not using that value. I could extend the schema to include some kind of sequential value.

                  This is what I've tried:

                  SELECT id, f.length, f2.length, (f.length - f2.length) AS difference
                  FROM foo f, foo f2

                  Thank you for the assist.

                  解决方案

                  This might help you (somewhat).

                  
                  select a.id, a.length, 
                  coalesce(a.length - 
                      (select b.length from foo b where b.id = a.id + 1), a.length) as diff
                  from foo a
                  
                  

                  这篇关于在SQL中从另一行数据中减去一行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                • <tfoot id='8145N'></tfoot>
                    <tbody id='8145N'></tbody>

                        <legend id='8145N'><style id='8145N'><dir id='8145N'><q id='8145N'></q></dir></style></legend>
                        <i id='8145N'><tr id='8145N'><dt id='8145N'><q id='8145N'><span id='8145N'><b id='8145N'><form id='8145N'><ins id='8145N'></ins><ul id='8145N'></ul><sub id='8145N'></sub></form><legend id='8145N'></legend><bdo id='8145N'><pre id='8145N'><center id='8145N'></center></pre></bdo></b><th id='8145N'></th></span></q></dt></tr></i><div id='8145N'><tfoot id='8145N'></tfoot><dl id='8145N'><fieldset id='8145N'></fieldset></dl></div>

                          <bdo id='8145N'></bdo><ul id='8145N'></ul>

                          1. <small id='8145N'></small><noframes id='8145N'>