【问题标题】:SQL query - add summary rowSQL 查询 - 添加汇总行
【发布时间】:2021-11-16 18:17:21
【问题描述】:

我正在尝试为我的 gradana sqlite 查询添加摘要行。

我目前的查询是这样的:

SELECT
date(close_date) AS "Date",
(SELECT COUNT(*) FROM trades WHERE close_profit > 0 AND date(c2.close_date) = date(close_date)) AS Wins,
(SELECT COUNT(*) FROM trades WHERE close_profit < 0 AND date(c2.close_date) = date(close_date)) AS Losses,
(SELECT AVG(close_profit * 100) FROM trades WHERE date(c2.close_date) = date(close_date)) AS "Avg",
(SELECT SUM(close_profit_abs) FROM trades WHERE date(c2.close_date) = date(close_date)) AS "Profit"
FROM trades c2 WHERE date(close_date) IS NOT NULL GROUP BY date(close_date) ORDER BY Date DESC

它会像这样输出表格:

Date         Wins      Losses      Avg     Profit
2021-09-23      1           0        3       68.8
2021-09-22      2           0      1.7       78.7
2021-09-21      5           0      4.8        538
2021-09-20     14           0      1.7        445

如何向此表添加摘要行? 像这样的:

Date         Wins      Losses      Avg     Profit
Total          22           0      2.8     1130.5   
2021-09-23      1           0        3       68.8
2021-09-22      2           0      1.7       78.7
2021-09-21      5           0      4.8        538
2021-09-20     14           0      1.7        445

摘要行应该是第一行。 胜利是所有胜利的总和。 损失是所有损失的总和。 Avg 是所有 Avgs 的平均值。 利润是所有利润的总和。

我尝试过这样的事情:

SELECT
  [close_date] = COALESCE(close_date, 'Total') AS date,
  [close_profit_abs] = SUM(close_profit_abs),
(SELECT COUNT(*) FROM trades WHERE close_profit > 0 AND date(c2.close_date) = date(close_date)) AS Wins,
(SELECT COUNT(*) FROM trades WHERE close_profit < 0 AND date(c2.close_date) = date(close_date)) AS Losses,
(SELECT AVG(close_profit * 100) FROM trades WHERE date(c2.close_date) = date(close_date)) AS "Avg",
(SELECT SUM(close_profit_abs) FROM trades WHERE date(c2.close_date) = date(close_date)) AS "Profit"
FROM trades c2 WHERE date(close_date) IS NOT NULL GROUP BY date(close_date) ORDER BY Date DESC

但这一切都被搞砸了……我的 sql 技能还不够…… 谁能帮我这个 ?我可以在这里找到更简单的指南,当我尝试将它们实现到我现有的代码中时,一切都会变得混乱。

任何帮助表示赞赏..谢谢

【问题讨论】:

  • 一些数据库有 - 汇总/多维数据集/分组。那将是最好的选择。否则,作为一种快速解决方法 - &lt;your query&gt; union all select sum(wins), sum(losses), avg(avg), sum(profit) from &lt;your_query&gt;

标签: sql grafana


【解决方案1】:

你可以这样做('summary row' UNION ALL 'your query'):

我改进了您的查询(我删除了您的子查询),如果结果不正确,请告诉我。

SELECT *
FROM (

    SELECT 
           'Total' AS "Date",
           count(CASE WHEN close_profit > 0 THEN 1 END) AS Wins,
           count(CASE WHEN close_profit < 0 THEN 1 END) AS Losses,
           AVG(close_profit * 100) AS "Avg",
           SUM(close_profit_abs)  AS "Profit"
    FROM trades
    WHERE close_date IS NOT NULL 

    UNION ALL

    SELECT
           date(close_date) AS "Date",
           count(CASE WHEN close_profit > 0 THEN 1 END) AS Wins,
           count(CASE WHEN close_profit < 0 THEN 1 END) AS Losses,
           AVG(close_profit * 100) AS "Avg",
           SUM(close_profit_abs)  AS "Profit"
    FROM trades
    WHERE close_date IS NOT NULL 
    GROUP BY date(close_date)) AS sq

ORDER BY "Date" DESC;

【讨论】:

  • 这太棒了,谢谢。 "ORDER BY Date DESC) AS sq"
  • 欢迎您@user3647376!我更新了我的查询。我拿出第一个ORDER BY,这里重要的是最后一个。我将“总计”添加到行摘要的Date 列。
猜你喜欢
  • 1970-01-01
  • 2013-12-20
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多