简体   繁体   English

多列完全外部联接

[英]Full outer join with multiple columns

I have two tables: one containing data with end of year information and another with end of quarter information for multiple products. 我有两个表:一个包含多个产品的年末信息数据,另一个包含季度末的信息数据。 I want to join them so that each row has information for one product per day: 我想加入他们,以便每一行每天都有关于一种产品的信息:

product_id | annual or quarter date | annual_info | quarterly_info

A full outer join like: 完整的外部联接,例如:

select a.date, b.date from 
annual_table a
full outer join quarterly_table q on q.product_id = a.product_id and q.date = a.date

does not work, since it only gives me the end of year dates: 不起作用,因为它只给我年末日期:

date       |date       |
-----------|-----------|
1996-12-31 |           |
1997-12-31 |           |
1998-12-31 |           |
1999-12-31 |           |
2000-12-31 |           |
2001-12-31 |           |
2002-12-31 |2002-12-31 |
2003-12-31 |2003-12-31 |
2004-12-31 |2004-12-31 |
2005-12-31 |2005-12-31 |
2006-12-31 |2006-12-31 |
2007-12-31 |2007-12-31 |

I thought a full outer join was also supposed to include rows that did not have a counterpart in the other table. 我以为完整的外部联接还应该包括在另一个表中没有对应项的行。

Annual table: 年表:

product_id  |date       |sales            |
------------|-----------|-----------------|
GHPRRB-S-US |1996-12-31 |48.5933895113959 |
GHPRRB-S-US |1997-12-31 |177.526458150594 |
GHPRRB-S-US |1998-12-31 |184.663          |
GHPRRB-S-US |1999-12-31 |276.333          |
GHPRRB-S-US |2000-12-31 |440.285          |
GHPRRB-S-US |2001-12-31 |298.466          |
GHPRRB-S-US |2002-12-31 |301.962          |
GHPRRB-S-US |2003-12-31 |391.93           |
GHPRRB-S-US |2004-12-31 |621.138          |
GHPRRB-S-US |2005-12-31 |420.929          |
GHPRRB-S-US |2006-12-31 |517.706          |
GHPRRB-S-US |2007-12-31 |475.089          |

Quarterly table: 季度表:

product_id  |date       |sales   |
------------|-----------|--------|
GHPRRB-S-US |2002-03-31 |68.97   |
GHPRRB-S-US |2002-06-30 |77.702  |
GHPRRB-S-US |2002-09-30 |80.123  |
GHPRRB-S-US |2002-12-31 |75.167  |
GHPRRB-S-US |2003-03-31 |68.97   |
GHPRRB-S-US |2003-06-30 |91.701  |
GHPRRB-S-US |2003-09-30 |103.085 |
GHPRRB-S-US |2003-12-31 |125.926 |
GHPRRB-S-US |2004-03-31 |130.28  |
GHPRRB-S-US |2004-06-30 |179.916 |
GHPRRB-S-US |2004-09-30 |163.52  |
GHPRRB-S-US |2004-12-31 |148.129 |
GHPRRB-S-US |2005-03-31 |115.843 |
GHPRRB-S-US |2005-06-30 |122.392 |
GHPRRB-S-US |2005-09-30 |124.545 |
GHPRRB-S-US |2005-12-31 |124.641 |
GHPRRB-S-US |2006-03-31 |127.33  |
GHPRRB-S-US |2006-06-30 |143.873 |
GHPRRB-S-US |2006-09-30 |136.842 |
GHPRRB-S-US |2006-12-31 |109.661 |
GHPRRB-S-US |2007-03-31 |97.253  |
GHPRRB-S-US |2007-06-30 |114.988 |
GHPRRB-S-US |2007-09-30 |128.189 |
GHPRRB-S-US |2007-12-31 |134.659 |

Just to close this out: I had a where clause that was restricting the results. 只是为了结束这一点:我有一个where子句来限制结果。 I pushed the WHERE clause into the ON conditions and I was able to see all the results. 我将WHERE子句推到ON条件中,就可以看到所有结果。

Thanks to @xQbert for spotting it! 感谢@xQbert发现它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM