簡體   English   中英

添加運行或累計總數

[英]Add running or cumulative total

我有以下查詢,它給了我預期的結果:

SELECT 
  total_orders,
  quantity,
  available_store_credits
FROM
  (
  SELECT
    COUNT(orders.id) as total_orders,
    date_trunc('year', confirmed_at) as year,
    date_trunc('month', confirmed_at) as month,
    SUM( quantity ) as quantity,
  FROM
    orders
    INNER JOIN (
      SELECT
        orders.id,
        sum(quantity) as quantity
      FROM
        orders
        INNER JOIN line_items ON line_items.order_id = orders.id
      WHERE
        orders.deleted_at IS NULL
        AND orders.status IN (
          'paid', 'packed', 'in_transit', 'delivered'
        )
      GROUP BY
        orders.id
    ) as order_quantity
      ON order_quantity.id = orders.id
  GROUP BY month, year) as orders_transactions
      
  FULL OUTER JOIN
  (
    SELECT
      date_trunc('year', created_at) as year,
      date_trunc('month', created_at) as month,
      SUM( ROUND( ( CASE WHEN amount_in_cents > 0 THEN amount_in_cents end) / 100, 2 )) AS store_credit_given,
      SUM( ROUND( amount_in_cents / 100, 2 )) AS available_store_credits
    FROM
      store_credit_transactions
    GROUP BY month, year
  ) as store_credit_results
    ON orders_transactions.month = store_credit_results.month

我想在available_store_credits旁邊再添加一列,它將計算available_store_credits運行總數
這些是我的試驗,但沒有奏效:

嘗試 #1

SELECT
  total_orders,
  quantity,
  available_store_credits,
  cum_amt
FROM
  (
  SELECT
    COUNT(orders.id) as total_orders,
    date_trunc('year', confirmed_at) as year,
    date_trunc('month', confirmed_at) as month,
    SUM( quantity ) as quantity,
  FROM
    orders
    INNER JOIN (
      SELECT
        orders.id,
        sum(quantity) as quantity
      FROM
        orders
        INNER JOIN line_items ON line_items.order_id = orders.id
      WHERE
        orders.deleted_at IS NULL
        AND orders.status IN (
          'paid', 'packed', 'in_transit', 'delivered'
        )
      GROUP BY
        orders.id
    ) as order_quantity
      ON order_quantity.id = orders.id
  GROUP BY month, year) as orders_transactions
      
  FULL OUTER JOIN
  (
    SELECT
      date_trunc('year', created_at) as year,
      date_trunc('month', created_at) as month,
      SUM( ROUND( ( CASE WHEN amount_in_cents > 0 THEN amount_in_cents end) / 100, 2 )) AS store_credit_given,
      SUM( ROUND( amount_in_cents / 100, 2 )) AS available_store_credits
      SUM( amount_in_cents ) OVER (ORDER BY date_trunc('month', created_at), date_trunc('year', created_at)) AS cum_amt
    FROM
      store_credit_transactions
    GROUP BY month, year
  ) as store_credit_results
    ON orders_transactions.month = store_credit_results.month

嘗試#2

SELECT 
  total_orders,
  quantity,
  available_store_credits,
  running_tot
FROM
  (
  SELECT
    COUNT(orders.id) as total_orders,
    date_trunc('year', confirmed_at) as year,
    date_trunc('month', confirmed_at) as month,
  FROM
    orders
    INNER JOIN (
      SELECT
        orders.id,
        sum(quantity) as quantity
      FROM
        orders
        INNER JOIN line_items ON line_items.order_id = orders.id
      WHERE
        orders.deleted_at IS NULL
        AND orders.status IN (
          'paid', 'packed', 'in_transit', 'delivered'
        )
      GROUP BY
        orders.id
    ) as order_quantity
      ON order_quantity.id = orders.id
  GROUP BY month, year) as orders_transactions
      
  FULL OUTER JOIN
  (
    SELECT
      date_trunc('year', created_at) as year,
      date_trunc('month', created_at) as month,
      SUM( ROUND( amount_in_cents / 100, 2 )) AS available_store_credits,
      SUM (available_store_creds) as running_tot
    FROM
      store_credit_transactions
      INNER JOIN (
        SELECT t0.id,
            (
             SELECT SUM( ROUND( amount_in_cents / 100, 2 )) as running_total
             FROM store_credit_transactions as t1 
             WHERE date_trunc('month', t1.created_at) <= date_trunc('month', t0.created_at)
            ) AS available_store_creds
            FROM store_credit_transactions AS t0
          
      ) as results
      ON results.id = store_credit_transactions.id
    GROUP BY month, year
  ) as store_credit_results
    ON orders_transactions.month = store_credit_results.month

對未公開的表定義和 Postgres 版本(假設當前的 Postgres 14)做出一些假設,應該這樣做:

SELECT total_orders, quantity, available_store_credits
     , sum(available_store_credits) OVER (ORDER BY month) AS cum_amt  -- HERE!!
FROM  (
   SELECT date_trunc('month', confirmed_at) AS month
        , count(*) AS total_orders
        , sum(quantity) AS quantity
   FROM  (
      SELECT o.id, o.confirmed_at, sum(quantity) AS quantity
      FROM   orders     o
      JOIN   line_items l ON l.order_id = o.id
      WHERE  o.deleted_at IS NULL
      AND    o.status IN ('paid', 'packed', 'in_transit', 'delivered')
      GROUP  BY 1
      ) o
   GROUP  BY 1
   ) orders_transactions
      
FULL   JOIN (
   SELECT date_trunc('month', created_at) AS month
        , round(sum(amount_in_cents) FILTER (WHERE amount_in_cents > 0) / 100, 2) AS store_credit_given
        , round(sum(amount_in_cents) / 100, 2) AS available_store_credits
   FROM   store_credit_transactions
   GROUP  BY 1
   ) store_credit_results USING (month)

假設您希望運行總和顯示在日期的每一行和順序中。

首先,我簡化並刪除了一些瑣碎的東西:

  • date_trunc('year', confirmed_at) as year,在您的查詢中是 100% 的冗余噪聲。 我刪除了它。

  • 正如另一個加入orders 也刪了。 假設orders.id定義為PK,我們可以進一步簡化。 看:

使用高級聚合FILTER 看:

簡化了其他幾個小位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM