Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 在alias上使用聚合函数?_Postgresql_Aggregate Functions - Fatal编程技术网

Postgresql 在alias上使用聚合函数?

Postgresql 在alias上使用聚合函数?,postgresql,aggregate-functions,Postgresql,Aggregate Functions,我想做一个查询,计算两列之间的差异。比如: SELECT a, b, a - b as "diff" FROM ... 现在,我想使用postgresql内置的stddev聚合函数计算“diff”列的stddev。我怎样才能做到这一点 谢谢 编辑: 实际查询如下: SELECT tr.date_start, tr.date_end, (((CASE when(tourney_summary.val_curr_con

我想做一个查询,计算两列之间的差异。比如:

 SELECT a,
        b,
        a - b as "diff"
  FROM ...
现在,我想使用postgresql内置的stddev聚合函数计算“diff”列的stddev。我怎样才能做到这一点

谢谢

编辑:

实际查询如下:

SELECT tr.date_start,
       tr.date_end,       
       (((CASE when(tourney_summary.val_curr_conv != 0) THEN tourney_summary.val_curr_conv * (tr.amt_won + tr.cnt_bounty * tourney_summary.amt_bounty) ELSE 0.0 END))) AS "amt_won_curr_conv",
          (((CASE when(tourney_summary.val_curr_conv != 0) THEN tourney_summary.val_curr_conv * (tourney_summary.amt_buyin + tourney_summary.amt_fee + tourney_summary.amt_rebuy * tr.cnt_rebuy + tourney_summary.amt_addon * tr.cnt_addon + tourney_summary.amt_bounty) ELSE 0.0 END))) AS "amt_buyin_ttl_curr_conv",
       ((((CASE when(tourney_summary.val_curr_conv != 0) THEN tourney_summary.val_curr_conv * (tr.amt_won + tr.cnt_bounty * tourney_summary.amt_bounty) ELSE 0.0 END))) - (((CASE when(tourney_summary.val_curr_conv != 0) THEN tourney_summary.val_curr_conv * (tourney_summary.amt_buyin + tourney_summary.amt_fee + tourney_summary.amt_rebuy * tr.cnt_rebuy + tourney_summary.amt_addon * tr.cnt_addon + tourney_summary.amt_bounty) ELSE 0.0 END)))) as net_amt_won,
       stddev((((CASE when(tourney_summary.val_curr_conv != 0) THEN tourney_summary.val_curr_conv * (tr.amt_won + tr.cnt_bounty * tourney_summary.amt_bounty) ELSE 0.0 END))) - (((CASE when(tourney_summary.val_curr_conv != 0) THEN tourney_summary.val_curr_conv * (tourney_summary.amt_buyin + tourney_summary.amt_fee + tourney_summary.amt_rebuy * tr.cnt_rebuy + tourney_summary.amt_addon * tr.cnt_addon + tourney_summary.amt_bounty) ELSE 0.0 END)))) as diff_std_dev

FROM tourney_summary,
     tourney_results tr
WHERE 
  tr.id_player=1
  AND tourney_summary.id_tourney = tr.id_tourney
  AND ((tourney_summary.id_gametype = 1)
       AND (((((((tourney_summary.id_table_type IN
                    (SELECT lttt.id_table_type
                     FROM tourney_table_type lttt
                     WHERE lttt.val_seats = 2))))))
             AND (((((tourney_summary.id_table_type IN
                        (SELECT lttt.id_table_type
                         FROM tourney_table_type lttt
                         WHERE position('S' IN lttt.val_speed) > 0))
                     OR (tourney_summary.id_table_type IN
                           (SELECT lttt.id_table_type
                            FROM tourney_table_type lttt
                            WHERE position('H' IN lttt.val_speed) > 0))))))))
       AND ((tourney_summary.date_start >= '2013/08/15 23:00:00')))
GROUP BY tr.date_start,
         tr.date_end,
         tourney_summary.val_curr_conv,
         tr.amt_won,
         tr.cnt_bounty,
         tourney_summary.amt_bounty,
         tourney_summary.amt_buyin,
         tourney_summary.amt_fee,
         tourney_summary.amt_rebuy,
         tr.cnt_rebuy,
         tourney_summary.amt_addon,
         tr.cnt_addon
ORDER BY tr.date_end DESC;
“a”和“b”表达式(带大小写的)很大。我不知道如何避免复制/粘贴。在任何情况下,对a-b表达式使用stddev都会返回一个空白列。我做错了什么


谢谢。

你几乎是自己回答的。计算差异的标准偏差:

 SELECT a,
        b,
        a - b as "diff",
        stddev(a - b) AS "diff_stddev"
  FROM ...
如果
a-b
是一个计算成本很高的操作,或者实际上是一个更复杂的表达式,则可以将其包装在子查询中:

 SELECT a, b, "diff", stddev("diff") AS diff_stddev
 FROM (
   SELECT a, b, a - b
   FROM ...
 ) x (a, b, "diff")

x
只是子查询表的一个一次性别名。

您几乎可以自己回答它。计算差异的标准偏差:

 SELECT a,
        b,
        a - b as "diff",
        stddev(a - b) AS "diff_stddev"
  FROM ...
如果
a-b
是一个计算成本很高的操作,或者实际上是一个更复杂的表达式,则可以将其包装在子查询中:

 SELECT a, b, "diff", stddev("diff") AS diff_stddev
 FROM (
   SELECT a, b, a - b
   FROM ...
 ) x (a, b, "diff")

x
只是子查询表的一次性别名。

使用cte也可以这样做

with cte1 as (
   select a, b, a - b as diff
   from ...
)
select
    a, b, diff, stddev(diff) as diff_stddev
from cte1

也可以使用cte进行此操作

with cte1 as (
   select a, b, a - b as diff
   from ...
)
select
    a, b, diff, stddev(diff) as diff_stddev
from cte1

注意你不需要大部分的
(…)
括号(这个查询是由ORM生成的吗?):你也可以将(…)子查询中的三个
组合成一个(并且可能使用
EXISTS(…)
),我认为查询可能是半自动生成的。注意你不需要大部分的
(…)
括号(这个查询是由ORM生成的吗?):您可以将(…)
子查询中的三个
组合成一个(并可能使用
EXISTS(…)
),我认为查询可能是半自动生成的。