从表中获取分组数据

发布于 2025-02-13 23:58:48 字数 1312 浏览 2 评论 0原文

我有一个名为交易的表 交易表具有以下模式:

id (int)
exchange_name (VARCHAR)
exchange_icon (VARCHAR)
trade_time (bigint)
price_quote (int)
price_usd (int)
trade_value (int)
base_asset_icon (VARCHAR)
qty (int)
quoteQty (int)
is_buyer_maker (boolean)
pair (VARCHAR)
base_asset_trade (VARCHAR)
quote_asset_trade (VARCHAR)

我需要这种格式的数据:

    base_asset_trade
    base_asset_icon
    sold: (number of times is_buyer_maker was **true** for this base asset)
    bought: (number of times is_buyer_maker was **false** for this base asset)
    sold_trade_value: (sum of trade_value for all where base asset is sold)
    bought_trade_value: (sum of trade_value for all where base asset is bought)
    price_usd
total_trades: (sum of bought and sold)

如何使用Postgres查询获得这些结果?

到目前为止,我已经尝试过:

SELECT base_asset_trade, trade_value, price_in_usd, base_asset_icon, is_buyer_maker  
FROM (SELECT * 
      FROM trades 
      WHERE  trade_time / 1000 > (extract(epoch from now()) - (86400)*1))
) AS derivedTable 
GROUP BY is_buyer_maker 

但这给出了几个错误。

样本输出:

base_asset: BTC
base_asset_icon: btc.png
sold: 48
bought: 56
sold_trade_value: 200000000
bought_trade_value: 25644300
price: 19000
total_trades: 104

I have a table named trades
The trades table has the following schema:

id (int)
exchange_name (VARCHAR)
exchange_icon (VARCHAR)
trade_time (bigint)
price_quote (int)
price_usd (int)
trade_value (int)
base_asset_icon (VARCHAR)
qty (int)
quoteQty (int)
is_buyer_maker (boolean)
pair (VARCHAR)
base_asset_trade (VARCHAR)
quote_asset_trade (VARCHAR)

I need data in the this format:

    base_asset_trade
    base_asset_icon
    sold: (number of times is_buyer_maker was **true** for this base asset)
    bought: (number of times is_buyer_maker was **false** for this base asset)
    sold_trade_value: (sum of trade_value for all where base asset is sold)
    bought_trade_value: (sum of trade_value for all where base asset is bought)
    price_usd
total_trades: (sum of bought and sold)

How can I get these results using postgres queries?

So far I have tried this:

SELECT base_asset_trade, trade_value, price_in_usd, base_asset_icon, is_buyer_maker  
FROM (SELECT * 
      FROM trades 
      WHERE  trade_time / 1000 > (extract(epoch from now()) - (86400)*1))
) AS derivedTable 
GROUP BY is_buyer_maker 

But this is giving several errors.

Sample output:

base_asset: BTC
base_asset_icon: btc.png
sold: 48
bought: 56
sold_trade_value: 200000000
bought_trade_value: 25644300
price: 19000
total_trades: 104

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2025-02-20 23:58:48

目前尚不清楚什么构成对或错,因此您在此处看到的案例表达可能需要校正,但是查询的结构应适用。您需要一个子句和聚合功能的组计数和总和。

SELECT
      base_asset_trade
    , base_asset_icon
    , count(CASE WHEN is_buyer_maker = 'sold' THEN 1 END) AS sold
    , count(CASE WHEN is_buyer_maker = 'bought' THEN 1 END) AS bought
    , sum(CASE WHEN is_buyer_maker = 'sold' THEN trade_value ELSE 0 END) AS sold_trade_value
    , sum(CASE WHEN is_buyer_maker = 'bought' THEN trade_value ELSE 0 END) AS bought_trade_value
    , price_usd
    , sum(trade_value) AS total_trades
FROM trades
GROUP BY
      base_asset_trade
    , base_asset_icon
    , price_usd

注意:在聚合函数中使用案例表达式时,它被称为“条件聚合”。

It is not clear what constitues true or false, so the case expressions you see here may need correction, but the structure of the query should apply. You need a group by clause and aggregation functions count and sum.

SELECT
      base_asset_trade
    , base_asset_icon
    , count(CASE WHEN is_buyer_maker = 'sold' THEN 1 END) AS sold
    , count(CASE WHEN is_buyer_maker = 'bought' THEN 1 END) AS bought
    , sum(CASE WHEN is_buyer_maker = 'sold' THEN trade_value ELSE 0 END) AS sold_trade_value
    , sum(CASE WHEN is_buyer_maker = 'bought' THEN trade_value ELSE 0 END) AS bought_trade_value
    , price_usd
    , sum(trade_value) AS total_trades
FROM trades
GROUP BY
      base_asset_trade
    , base_asset_icon
    , price_usd

Note: when using case expressions inside an aggregation function it is known as "conditional aggregation".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文