1

2 つの列の差を計算するクエリを作成したいと考えています。何かのようなもの:

 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" 式 (CASE のあるもの) が大きいです。そして、コピー/貼り付けを回避する方法がわかりません。いずれにせよ、ab 式で stddev を使用すると、空白の列が返されます。私は何を間違っていますか?

ありがとう。

4

2 に答える 2

2

cteでこれを行うことも可能です

with cte1 as (
   select a, b, a - b as diff
   from ...
)
select
    a, b, diff, stddev(diff) as diff_stddev
from cte1
于 2013-08-17T16:30:58.913 に答える