在SQL中汇总不同货币的销售额

时间:2012-01-17 19:55:50

标签: mysql sql

这是对我之前提出的问题的跟进:Using a GROUP BY statement to sum rows。我有一张销售不同货币的表格,我使用GROUP BY语句来总结每个标题的数字。

mysql> SELECT
       title, 
       SUM(us_earnings_usd) AS usd,
       SUM(cad_earnings_cad) AS cad,
       SUM(uk_earnings_gbp) AS gbp,
       SUM(swedish_earnings_skk) AS skk
       FROM raw_financials 
       WHERE date="2012-12-01"
       GROUP BY title

+--------+-----------------+------------------+------------------+------------------+
| title  | us_earnings_usd | cad_earnings_cad |swedish_earnings_ |  uk_earnings_gbp |
+--------+-----------------+------------------+------------------+------------------+
| Gamers |          7.7500 |           4.0000 |           1.0000 |           2.0000 |
+--------+-----------------+------------------+------------------+------------------+

最后,我需要总结这些字段以获得美元的总销售额。为此,我有一个名为exchange_rates的附加表。这是可以查询的方式:

mysql> SELECT currency, conversion_to_usd FROM exchange_rates WHERE date="2011-12-01";

+----------+-------------------+
| currency | conversion_to_usd |
+----------+-------------------+
| AUD      |           0.98542 |
| CAD      |           0.95940 |
| CHF      |           1.05235 |
| DKK      |           0.17372 |
| EUR      |           1.29400 |
| GBP      |           1.54223 |
| NOK      |           0.16579 |
| NZD      |           0.74442 |
| SEK      |           0.14190 |
| USD      |           1.00000 |
+----------+-------------------+

我如何使用SQL来组合这些:

total_earnings_in_usd = 7.75 (earnings in usd) *1.00 (conversion from usd to usd) 
                        + 4.00 (earnings in cad) *0.95 (conversion from cad to usd) 
                        + 1.00 (earnings in sek) *0.14 (conversion from sek to usd)
                        + 2.00 (earnings in gbp) *1.54 (conversion from gbp to usd) 
                      = $14.77 USD

更新:我已在问题中更新了SQL。

3 个答案:

答案 0 :(得分:1)

更新后的评论

SELECT  title, 
        partner_share_currency,
        SUM(us_earnings_usd) AS usd,
        SUM(cad_earnings_cad) AS cad,
        SUM(us_earnings_usd) + SUM(cad_earnings_cad*CAD) +
        SUM(uk_earnings_gbp*GBP) + SUM(swedish_earnings_skk*SEK) total_earnings_in_usd
FROM raw_financials rf
LEFT JOIN ( SELECT  date, 
                    MIN(CASE WHEN  currency = 'CAD' THEN conversion_to_usd END) CAD,
                    MIN(CASE WHEN  currency = 'GBP' THEN conversion_to_usd END) GBP,
                    MIN(CASE WHEN  currency = 'SEK' THEN conversion_to_usd END) SEK
            FROM exchange_rates 
            WHERE currency IN ('CAD','GBP','SEK')
            GROUP BY date) er
ON rf.date = er.date
WHERE title LIKE '%Gamers%' AND rf.date= '2012-12-01'
GROUP BY title

答案 1 :(得分:1)

我在下面测试了SQL(SQL Server中的T-SQL)。它返回11.5876作为grand_total。这对你好吗?

create table #raw_financials (
    title varchar(20)
    ,partner_share_currency varchar(20)
    ,us_earnings_usd decimal(15, 5)
    ,cad_earnings_cad decimal(15, 5)
    ,date datetime
)
insert into #raw_financials values ('Gamers', 'USD', 3.25, 0, '2012-12-01')
insert into #raw_financials values ('Gamers', 'CAD', 0, 4, '2012-12-01')
insert into #raw_financials values ('Gamers', 'USD', 4.5, 0, '2012-12-01')

create table #exchange_rates (
    currency varchar(20)
    ,conversion_to_usd decimal(15, 5)
    ,date datetime
)
insert into #exchange_rates values ('CAD', 0.95940, '2012-12-01')
insert into #exchange_rates values ('USD', 1, '2012-12-01')

SELECT
rf.title,
--partner_share_currency,
SUM((rf.us_earnings_usd + rf.cad_earnings_cad) * er.conversion_to_usd) AS grand_total
FROM #raw_financials rf
INNER JOIN #exchange_rates er
ON er.currency = rf.partner_share_currency
AND er.date = rf.date
WHERE rf.title LIKE '%Gamers%' AND rf.date='2012-12-01'
GROUP BY rf.title

但我从未使用过MySQL,结果可能会有所不同。

更新:以下是GBP和SEK的另一个查询。它在我的T-SQL中返回14.81396:

create table #raw_financials (
    title varchar(20)
    ,partner_share_currency varchar(20)
    ,us_earnings_usd decimal(15, 5)
    ,cad_earnings_cad decimal(15, 5)
    ,uk_earnings_gbp decimal(15, 5)
    ,swedish_earnings_skk decimal(15, 5)
    ,date datetime
)
insert into #raw_financials values ('Gamers', 'USD', 3.25, 0, 0, 0, '2012-12-01')
insert into #raw_financials values ('Gamers', 'CAD', 0, 4, 0, 0, '2012-12-01')
insert into #raw_financials values ('Gamers', 'USD', 4.5, 0, 0, 0, '2012-12-01')
insert into #raw_financials values ('Gamers', 'GBP', 0, 0, 2, 0, '2012-12-01')
insert into #raw_financials values ('Gamers', 'SEK', 0, 0, 0, 1, '2012-12-01')

create table #exchange_rates (
    currency varchar(20)
    ,conversion_to_usd decimal(15, 5)
    ,date datetime
)
insert into #exchange_rates values ('CAD', 0.95940, '2012-12-01')
insert into #exchange_rates values ('USD', 1, '2012-12-01')
insert into #exchange_rates values ('GBP', 1.54223, '2012-12-01')
insert into #exchange_rates values ('SEK', 0.14190, '2012-12-01')

SELECT
rf.title,
SUM((rf.us_earnings_usd + 
     rf.cad_earnings_cad + 
     rf.uk_earnings_gbp + 
     rf.swedish_earnings_skk
    ) * er.conversion_to_usd) AS grand_total
FROM #raw_financials rf
INNER JOIN #exchange_rates er
ON er.currency = rf.partner_share_currency
AND er.date = rf.date
WHERE rf.title LIKE '%Gamers%' AND rf.date='2012-12-01'
GROUP BY rf.title

答案 2 :(得分:0)

SELECT
    t.title, 
    t.usd,
    t.cad,
    t.gbp,
    t.skk,
    t.usd + t.cad*er_cad.conversion_to_usd + t.gbp*er_gbp.conversion_to_usd + t.skk*er_skk.conversion_to_usd AS total
FROM
(
    SELECT
        title, 
        SUM(us_earnings_usd) AS usd,
        SUM(cad_earnings_cad) AS cad,
        SUM(uk_earnings_gbp) AS gbp,
        SUM(swedish_earnings_skk) AS skk,
        FROM raw_financials 
        WHERE date="2012-12-01"
        GROUP BY title
) t
INNER JOIN exchange_rates er_cad ON er_cad.date = t.date and er_cad.currency = 'CAD'
INNER JOIN exchange_rates er_gbp ON er_gbp.date = t.date and er_gbp.currency = 'GBP'
INNER JOIN exchange_rates er_skk ON er_skk.date = t.date and er_skk.currency = 'SKK'

无论如何,我认为您的销售表有问题。您应该只有一个earnings列(不是每个货币的单独一列)。如您所见,对于每一行,您必须只填充一列具有实际值,其余为零。只需partner_share_currency列和通用earnings列即可。