MySQL总结了两个相反的货币对

时间:2015-04-23 17:36:24

标签: mysql sql

我在表格中有以下买卖货币对及其价值,我试图写一个小的选择语句,得到每对的总数,但是当这些货币对具有对立面时,它们可以相互扣除,例如,卖出任何欧元都没有意义,因为它可以用来取消部分买入欧元。因此两条线GBP / EUR,EUR / GBP将成为: 请注意,这些对可能不仅匹配欧元/英镑,因此无法在

中进行硬编码
EUR 852.07  GBP -636.1

(也许专注于减少购买量是要走的路?)

我很乐意给你一个例子,但我完全陷入困境!任何想法都非常感谢。

Buy             Sell
ccy  amt        ccy  amt
EUR, 1409.7600, AUD, -1965.4900
GBP, 2801.8500, AUD, -5446.5100
EUR, 1116.2800, CAD, -1472.1500
EUR, 4862.2000, CHF, -5013.9000
GBP, 3937.6700, CHF, -5661.5700
EUR, 2066.8900, DKK, -15505.0000
GBP, 1688.7200, DKK, -17640.6400
**
GBP, 1986.4400, EUR, -2778.7500
EUR, 3630.8200, GBP, -2622.5400
**
EUR, 358.4100,  NOK, -3046.6600
GBP, 2865.9800, NOK, -33965.5900
EUR, 1574.9800, SEK, -14746.0600
GBP, 1511.3100, SEK, -19727.5900
EUR, 724.3600,  USD, -777.8900
GBP, 34.7400,   USD, -52.0200


ccy = currency char(3)
amt = amount decimal

最后的结果应该是这样的。

Buy             Sell
ccy  amt        ccy  amt
EUR, 1409.7600, AUD, -1965.4900
GBP, 2801.8500, AUD, -5446.5100
EUR, 1116.2800, CAD, -1472.1500
EUR, 4862.2000, CHF, -5013.9000
GBP, 3937.6700, CHF, -5661.5700
EUR, 2066.8900, DKK, -15505.0000
GBP, 1688.7200, DKK, -17640.6400
**
EUR, 852.07     GBP, -636.1
**
EUR, 358.4100,  NOK, -3046.6600
GBP, 2865.9800, NOK, -33965.5900
EUR, 1574.9800, SEK, -14746.0600
GBP, 1511.3100, SEK, -19727.5900
EUR, 724.3600,  USD, -777.8900
GBP, 34.7400,   USD, -52.0200

4 个答案:

答案 0 :(得分:2)

我不得不稍微更改你的列名,以反映同一记录中有多个ccy和amt的事实。

SELECT
a.buy_ccy,
SUM(a.buy_amt) + COALESCE(SUM(b.sell_amt),0) as buy_amt,
a.sell_ccy,
SUM(a.sell_amt) + COALESCE(SUM(b.buy_amt),0) as sell_amt
FROM
transactions as a
LEFT OUTER JOIN transactions as b ON (a.buy_ccy = b.sell_ccy AND a.sell_ccy = b.buy_ccy)
GROUP BY
a.buy_ccy,
b.buy_ccy,
a.sell_ccy,
b.sell_ccy
HAVING buy_amt > 0;

示例: http://sqlfiddle.com/#!9/f2006/3

答案 1 :(得分:1)

试试这个:

    SELECT SUM( CASE 
          WHEN `ccy_buy` = 'EUR' AND `ccy_sell` = 'GBP' THEN `amt_buy`
          WHEN `ccy_sell` = 'EUR' AND `ccy_buy` = 'GBP' THEN `amt_sell`
       END
      ) AS `ccy1`,
      SUM( CASE 
          WHEN `ccy_buy` = 'GBP' and `ccy_sell` = 'EUR' THEN `amt_buy`
          WHEN `ccy_sell` = 'GBP' and `ccy_buy` = 'EUR' THEN `amt_sell`
       END
      ) AS `ccy2`
    FROM `test`

您可能希望将其包装到存储过程中并将两种货币作为参数传递,如下所示:

DELIMITER ;;
CREATE PROCEDURE `sp_compareCurrencyPairs`(IN pCur1 CHAR(3), IN pCur2 CHAR(3))
    READS SQL DATA
BEGIN
    SELECT SUM( CASE 
          WHEN `ccy_buy` = pCur1 AND `ccy_sell` = pCur2 THEN `amt_buy`
          WHEN `ccy_sell` = pCur1 AND `ccy_buy` = pCur2 THEN `amt_sell`
       END
      ) AS `ccy1`,
      SUM( CASE 
          WHEN `ccy_buy` = pCur2 AND `ccy_sell` = pCur1 THEN `amt_buy`
          WHEN `ccy_sell` = pCur2 AND `ccy_buy` = pCur1 THEN `amt_sell`
       END
      ) AS `ccy2`
    FROM `test`;
END;;
DELIMITER ;

然后像这样使用它:

CALL `sp_compareCurrencyPairs`('GBP', 'EUR');

答案 2 :(得分:0)

尝试这样的事情并假设你正在使用PHP ......

$sql = "
select
  sum( 
    case 
      when buy_currency = '$currency_one' then buy_amount
      when sell_currency = '$currency_one' then sell_amount
   end
  ) as first_currency,
  sum( 
    case 
      when buy_currency = '$currency_twp' then buy_amount
      when sell_currency = '$currency_two' then sell_amount
   end
  ) as second_currency
from test";

假设架构像

CREATE TABLE `test` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `buy_currency` VARCHAR(50) NOT NULL DEFAULT '0',
    `buy_amount` VARCHAR(50) NOT NULL DEFAULT '0',
    `sell_currency` VARCHAR(50) NOT NULL DEFAULT '0',
    `sell_amount` VARCHAR(50) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=6
;

答案 3 :(得分:0)

http://sqlfiddle.com/#!9/d5198/1

SELECT  
    IF(buy_ccy>sell_ccy,buy_ccy,sell_ccy) as buy_ccy,
    SUM(IF(buy_ccy>sell_ccy,buy_amt,sell_amt)) as buy_amt,
    IF(buy_ccy>sell_ccy,sell_ccy,buy_ccy) as sell_ccy,
    SUM(IF(buy_ccy>sell_ccy,sell_amt,buy_amt)) as sell_amt
FROM EXCHANGE
GROUP BY IF(buy_ccy>sell_ccy,CONCAT(buy_ccy,sell_ccy),CONCAT(sell_ccy,buy_ccy))