简体   繁体   English

如何对两个字段求和

[英]how to sum two fields

I'm trying to perform Arithmetic Operators and this is my query , 我正在尝试执行算术运算符,这是我的查询,

SELECT

    GLB.BEGIN_BALANCE_DR_BEQ + GLB.BEGIN_BALANCE_CR_BEQ AS ACTIVITY,
    GLB.PERIOD_NET_DR + GLB.PERIOD_NET_CR AS BEG_BALANCE ,
    ACTIVITY + BEG_BALANCE AS END_BALANCE,
    SUM(ACTIVITY) AS TOTAL_ACT,
    SUM(BEG_BALANCE) AS TOTAL_BEG_BALANCE,
    SUM(END_BALANCE) AS TOTAL_END_BALANCE, 

But it didn't work , Can anyone guide me how can I fix it ?! 但是它没有用,谁能指导我如何解决它?

You can't use alias in the same select level (and you can't use aggregation level and not aggregated column without a proper group by) in yor case 在这种情况下,您不能在相同的选择级别中使用别名(也不能在没有适当分组依据的情况下使用聚合级别和聚合列)

you should use a subquery for a fast use of alias eg: 您应该使用子查询来快速使用别名,例如:

  SELECT 
      SUM(ACTIVITY) AS TOTAL_ACT,
      SUM(BEG_BALANCE) AS TOTAL_BEG_BALANCE,
      SUM(END_BALANCE) AS TOTAL_END_BALANCE
      FROM( 
        SELECT

        GLB.BEGIN_BALANCE_DR_BEQ + GLB.BEGIN_BALANCE_CR_BEQ AS ACTIVITY,
        GLB.PERIOD_NET_DR + GLB.PERIOD_NET_CR AS BEG_BALANCE ,
        ACTIVITY + BEG_BALANCE AS END_BALANCE
        FROM My_TABLE 
      ) T

It's not valid for aliases assigned in the SELECT list of the query to be referenced elsewhere in the SELECT list or in the WHERE clause of the same query. 在查询的SELECT列表中分配的别名要在SELECT列表的其他位置或同一查询的WHERE子句中引用是无效的。 (MySQL does have extensions which allows a column alias to be referenced in ORDER BY and HAVING.) (MySQL确实具有扩展名,该扩展名允许在ORDER BY和HAVING中引用列别名。)

If we want to reference an alias in the SELECT list, one option is to make the query into an inline view. 如果要在SELECT列表中引用别名,一种选择是使查询进入内联视图。 And an outer query can reference an alias as a columname. 外部查询可以将别名引用为一列。 As a simple example... 作为一个简单的例子...

 SELECT c.myalias
      , c.fee
      , c.myalias + c.fee AS grand_tot
   FROM ( SELECT t.somecol + t.othercol  AS `myalias`
               , t.fi + t.fo + t.fum     AS `fee`
            FROM t
        ) c

For performance reasons, we would typically avoid doing this, due to the cost of materializing the inline view as a derived table. 出于性能方面的考虑,由于将内联视图实现为派生表的成本很高,因此我们通常会避免这样做。 It would be much more efficient to avoid the inline view, and reference columns that are available, even if we have to repeat expressions. 避免使用内联视图和可用的引用列会更加有效,即使我们必须重复表达式也是如此。

 SELECT t.somecol + t.othercol                       AS `myalias`
      , t.fi + t.fo + t.fum                          AS `fee`
      , t.somecol + t.othercol + t.fi + t.fo + t.fum AS `grand_tot`
   FROM t 

It's not clear what resultset twe are needing to return. 尚不清楚我们需要返回什么结果集。

If we want just the totals, I'd avoid an inline view, and just bite the bullet and use expressions that reference columns that are available in the SELECT list. 如果我们只想要总数,我会避免使用内联视图,而只是硬着头皮,使用引用SELECT列表中可用列的表达式。 It seems like an odd resultset to return: 返回的结果集似乎很奇怪:

SELECT SUM( GLB.BEGIN_BALANCE_DR_BEQ 
          + GLB.BEGIN_BALANCE_CR_BEQ
       ) AS TOTAL_ACT
     , SUM( GLB.PERIOD_NET_DR
          + GLB.PERIOD_NET_CR
       ) AS TOTAL_BEG_BALANCE
     , SUM( GLB_GLB.BEGIN_BALANCE_DR_BEQ 
          + GLB.BEGIN_BALANCE_CR_BEQ 
          + GLB.PERIOD_NET_DR
          + GLB.PERIOD_NET_CR
       ) AS TOTAL_END_BALANCE
  FROM ... 

You are mixing two types of operations in this query. 您正在此查询中混合两种类型的操作。 The first three lines are single row operations and the last three are multi row operations. 前三行是单行操作,后三行是多行操作。 As it's written you are asking for value of GLB.BEGIN_BALANCE_DR_BEQ + GLB.BEGIN_BALANCE_CR_BEQ AS ACTIVITY for each row, and the sum of ACTIVITY across all rows. 在撰写本文时,您需要为每行要求GLB.BEGIN_BALANCE_DR_BEQ + GLB.BEGIN_BALANCE_CR_BEQ AS ACTIVITY值,以及所有行的ACTIVITY的总和。

If you want the sum of each field across all rows then you have to wrap each call in the sum(...) function. 如果要跨所有行的每个字段的总和,则必须将每个调用包装在sum(...)函数中。 If you want a mix of single and multi row fields then you have to split them into two queries. 如果要混合使用单行字段和多行字段,则必须将它们分为两个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM