使用MySQL创建顶级列表

时间:2012-02-24 17:05:19

标签: mysql select join union

我想根据交易表找出谁拥有最多的利润。 (顶级名单)

Table: Transactions

    +--------------+
    | id           |
    +--------------+
    | buy_user_id  |
    +--------------+
    | sell_user_id |
    +--------------+
    | amount       |
    +--------------+
    | price        |
    +--------------+

此表包含有关在两个人之间进行交换时发生的交易的信息。 “buy_user_id”是买方,而“sell_user_id”是卖方。

“金额”是指购买了多少股票,价格是以什么价格(以美元计)。然后营业额(金额*价格)。

我想弄清楚哪些用户获利最多。如果您将总利润减去总损失,则最终结果应为0。

e.g:


    +---+---------+--------+
    | # | User ID | Profit |
    +---+---------+--------+
    | 1 | 13      | +1200  |
    +---+---------+--------+
    | 2 | 52      | +300   |
    +---+---------+--------+
    | 3 | 29      | -500   |
    +---+---------+--------+
    | 4 | 72      | -1000  |
    +---+---------+--------+

建议?

以下是完整示例,首先是事务表:


    +----+-------------+--------------+--------+-------+
    | id | buy_user_id | sell_user_id | amount | price |
    +----+-------------+--------------+--------+-------+ 
    | 1  | 13          | 72           | 1000   | 0.01  | $10 paid by 13 for 1000 stocks   (now 13 has $10 loss while 72 has $10 profit)
    +----+-------------+--------------+--------+-------+
    | 2  | 72          | 13           | 1000   | 1.01  | $1010 paid by 72 for 1000 stocks (now 72 has $1000 loss while 13 has $1000 profit)
    +----+-------------+--------------+--------+-------+
    | 3  | 13          | 72           | 500    | 0.02  | $10 paid by 13 for 500 stocks    (now 72 has $990 loss while 13 has $990 profit)
    +----+-------------+--------------+--------+-------+
    | 4  | 72          | 13           | 100    | 5.10  | $510 paid by 72 for 100 stocks   (now 72 has $1500 loss while 13 has $1500 profit)
    +----+-------------+--------------+--------+-------+
    | 5  | 29          | 13           | 400    | 1.25  | $500 paid by 29 for 400 stocks   (now 29 has $500 loss while 13 has $2000 profit)

此示例应生成此结果:


    +---+---------+--------+
    | # | User ID | Profit |
    +---+---------+--------+
    | 1 | 13      | +2000  |
    +---+---------+--------+
    | 2 | 72      | -1500  |
    +---+---------+--------+
    | 3 | 29      | -500   |
    +---+---------+--------+

实现这一目标的最佳方法是什么?这有意义吗?

我的尝试:

SELECT sell_user_id as user_id, SUM(amount*price) as amount, 'sell' as type 
FROM exchange_transactions 
GROUP BY sell_user_id 
UNION 
SELECT buy_user_id as user_id, SUM(amount*price) as amount, 'buy' as type 
FROM exchange_transactions 
GROUP BY buy_user_id

2 个答案:

答案 0 :(得分:1)

SELECT user_id
     , SUM(profit) AS profit
     , SUM(stock_balance) AS stock_balance
FROM
    ( SELECT sell_user_id AS user_id
           , +SUM(amount*price) AS profit
           , -SUM(amount) AS stock_balance
      FROM exchange_transactions 
      GROUP BY sell_user_id
    UNION ALL
      SELECT buy_user_id
          , -SUM(amount*price)
          , +SUM(amount) 
      FROM exchange_transactions 
      GROUP BY buy_user_id
    ) AS tmp
GROUP BY user_id
ORDER BY profit DESC 

答案 1 :(得分:0)

我认为以下内容可以帮助您更接近您所寻找的内容:

SELECT USER_ID, SUM(AMOUNT) AS PROFIT_LOSS FROM
(SELECT sell_user_id as user_id,
   SUM(amount * price) as amount
   FROM exchange_transactions
   GROUP BY sell_user_id
 UNION 
 SELECT buy_user_id as user_id,
   SUM(amount * price * -1) as amount
   FROM exchange_transactions
   GROUP BY buy_user_id)
ORDER BY PROFIT_LOSS DESC;

分享并享受。