找到每个组合事件的平均金额

时间:2015-01-03 21:52:13

标签: mysql sql

我有一张桌子(table1):

| Country1 | Country2 |
  Canada      USA
  Canada      Mexico
  USA         Mexico
  USA         Canada
.
.
.
etc

然后我有另一张桌子(桌子2):

| Country | Date | Amount |
  Canada    01-01  1.00
  Canada    01-02  0.23
  USA       01-01  2.67
  USA       01-02  5.65
  USA       01-03  8.00
.
.
.
etc

我需要一个将两个表组合成这样的表的查询:

| Country1 | Average_amount_when_combined_with_country2| Country2 | Average_amount_when_combined_with_country1 |
  Canada                       0.615                        USA                      4.16
  USA                          4.16                         Canada                   0.615

正在发生的事情是当国家1出现在第一个表中的国家2时我希望在国家2合并时获得国家1的平均金额,然后反之亦然,国家1的国家1的平均值组合。我尝试了不同的连接技术,但不能让它工作,但现在我认为我不能真正做任何传统的连接,我将需要使用子查询的组合。只有当两个国家出现在同一个日期时,我才完全坚持如何获得平均值。这个查询尽可能接近,但问题是这只是得到整个国家的平均值,而不是两个国家组合时的平均值。

select country1, (select avg(amount) from table2 where country = country1) ,country2,(select avg(amount) from table2 where country = country2)
from table1

3 个答案:

答案 0 :(得分:1)

以下SELECT应解决您的问题,或至少应该让您知道它是如何工作的。

SELECT  t1.Country1, 
        t1.Country2, 
        (
            SELECT  AVG(Amount) 
            FROM    table2 t2 
            WHERE   t2.Country = t1.Country1 AND 
                    EXISTS
                    (
                        SELECT 1      
                        FROM   table2 t3 
                        WHERE  t3.Country = t1.Country2 AND 
                               t2.Date = t3.Date
                    )
        ) Avg1, 
        (
            SELECT  AVG(Amount) 
            FROM    table2 t2 
            WHERE   t2.Country = t1.Country2 AND 
                    EXISTS
                    (
                        SELECT 1      
                        FROM   table2 t3 
                        WHERE  t3.Country = t1.Country1 AND 
                               t2.Date = t3.Date
                    )
        ) Avg2
FROM table1 t1

查看结果here

答案 1 :(得分:1)

这会带来相同的信息,但更合乎逻辑的方式,在我看来(你的预期输出在两行显示相同的内容,只是相反)。

select t2a.country as country1,
       t2b.country as country2,
       avg(t2b.amount) as avg_amount
  from table2 t2a
  join table2 t2b
    on t2a.date = t2b.date
  join table1 t1
    on t2a.country = t1.country1
   and t2b.country = t1.country2
 group by t2a.country, t2b.country

小提琴: http://sqlfiddle.com/#!2/34968/2/0

输出:

| COUNTRY1 | COUNTRY2 | AVG_AMOUNT |
|----------|----------|------------|
|   Canada |      USA |       4.16 |
|      USA |   Canada |      0.615 |

答案 2 :(得分:1)

如果我理解正确,您需要在进行任何加入之前按国家/地区汇总table2。否则,您将面临笛卡尔积的风险。在MySQL中,您将使用两个子查询来处理此问题:

select t1.country, t2_1.amount as avg1, t2.country, t2_2.amount
from table1 t1 join
     (select t2.country, avg(amount) as amount
      from table2 t2
      group by t2.country
     ) t2_1
     on t1.country1 = t2_1.country join
     (select t2.country, avg(amount) as amount
      from table2 t2
      group by t2.country
     ) t2_2
     on t1.country2 = t2_2.country;

在其他数据库中,您可能会使用公用表表达式(CTE)来避免重复的子查询。