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

        <small id='SjKB9'></small><noframes id='SjKB9'>

        如何在sql server中获取一列的运行总和

        How to get running sum of a column in sql server(如何在sql server中获取一列的运行总和)
        • <bdo id='pAZ9R'></bdo><ul id='pAZ9R'></ul>
          <i id='pAZ9R'><tr id='pAZ9R'><dt id='pAZ9R'><q id='pAZ9R'><span id='pAZ9R'><b id='pAZ9R'><form id='pAZ9R'><ins id='pAZ9R'></ins><ul id='pAZ9R'></ul><sub id='pAZ9R'></sub></form><legend id='pAZ9R'></legend><bdo id='pAZ9R'><pre id='pAZ9R'><center id='pAZ9R'></center></pre></bdo></b><th id='pAZ9R'></th></span></q></dt></tr></i><div id='pAZ9R'><tfoot id='pAZ9R'></tfoot><dl id='pAZ9R'><fieldset id='pAZ9R'></fieldset></dl></div>

              <legend id='pAZ9R'><style id='pAZ9R'><dir id='pAZ9R'><q id='pAZ9R'></q></dir></style></legend>

            • <tfoot id='pAZ9R'></tfoot>

                  <small id='pAZ9R'></small><noframes id='pAZ9R'>

                    <tbody id='pAZ9R'></tbody>
                1. 本文介绍了如何在sql server中获取一列的运行总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Bills 表中有一个名为 Qty 的列我想要一个列显示 Qty 列的运行总和,如下所示:

                  Hi I have a column with name Qty from table Bills i want a column that show the running sum of Qty column like this :

                  Qty   Run_Sum
                  1      1
                  2      3
                  3      6
                  4      10
                  5      15
                  

                  建议我一些合适的方法来运行一些谢谢

                  Suggest me some appropriate method to make running some thankx

                  推荐答案

                  SQLFiddle demo

                  SELECT Qty,
                  SUM(Qty) OVER (ORDER BY Qty) Run_Sum
                  FROM t ORDER BY Qty
                  

                  对于 2012 年之前的 SQLServer:

                  For SQLServer prior to 2012:

                  select Qty,
                  (select sum(Qty) from t where Qty<=t1.Qty)
                  from t t1 order by Qty
                  

                  SQLFiddle 演示

                  或者你也可以不用子查询:

                  Or also you can do it without subquery:

                  select t1.Qty, sum(t2.Qty)
                  from t t1 
                  join t t2 on (t1.Qty>=t2.Qty)
                  group by t1.Qty
                  order by t1.Qty
                  

                  SQLFiddle 演示

                  这篇关于如何在sql server中获取一列的运行总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)

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

                          <legend id='LDNB3'><style id='LDNB3'><dir id='LDNB3'><q id='LDNB3'></q></dir></style></legend>
                          <tfoot id='LDNB3'></tfoot>

                              <tbody id='LDNB3'></tbody>