简体   繁体   English

将每种行类型的一个表与其他表联接

[英]Join One Table with Other Table for Every Row Type

My First Table and its data is : 我的第一张表及其数据是:

DECLARE @TempTableA TABLE (FinYearVal VARCHAR(9))

FinYearVal
----------
2007-2008
2008-2009
2009-2010
2010-2011
2011-2012
2012-2013
2013-2014
2014-2015

Then I have another table with data as : 然后我有另一个表,数据为:

DECLARE @TempTableB TABLE (Category VARCHAR(10), FinYear VARCHAR(9), AMOUNT NUMERIC(18,10))

Category   FinYear       AMOUNT
---------- ------------- ----------
A          2013-2014     100.0000
A          2014-2015     200.0000
B          2012-2013     100.0000
B          2013-2014     200.0000
B          2014-2015     300.0000
B          2015-2016     400.0000
C          2011-2012     100.0000
C          2012-2013     200.0000

I want my Table1 to be Right Joined with Table2 for Every Category, just as we would separately Right Join and union the Data. 我希望每个类别的表1与表2正确连接,就像我们分别对数据进行右连接和联合一样。
The Expected Result is : 预期结果是:

Category   FinYearVal     AMOUNT
---------- ----------     ----------
A          2007-2008      0.0000
A          2008-2009      0.0000
A          2009-2010      0.0000
A          2010-2011      0.0000
A          2011-2012      0.0000
A          2012-2013      0.0000
A          2013-2014      100.0000
A          2014-2015      200.0000
B          2007-2008      0.0000
B          2008-2009      0.0000
B          2009-2010      0.0000
B          2010-2011      0.0000
B          2011-2012      0.0000
B          2012-2013      100.0000
B          2013-2014      200.0000
B          2014-2015      300.0000
C          2007-2008      0.0000
C          2008-2009      0.0000
C          2009-2010      0.0000
C          2010-2011      0.0000
C          2011-2012      100.0000
C          2012-2013      200.0000
C          2013-2014      0.0000
C          2014-2015      0.0000

NOTE: My Table2 has many Categories where I would dynamically choose how many categories I want to be joined in the Query. 注意:我的Table2有很多类别,我可以在其中动态选择要在查询中加入的类别。

Try this 尝试这个

SELECT C.Category,A.FinYearVal, ISNULL(B.AMOUNT,0) AS AMOUNT
FROM @TempTableA A
CROSS JOIN (SELECT DISTINCT Category FROM @TempTableB) C
LEFT JOIN @TempTableB B ON B.Category = C.Category AND B.FinYear = A.FinYearVal
SELECT t.FinYearVal, t.Category, ISNULL(ttb.AMOUNT,0)
FROM (
    SELECT tta.FinYearVal, d.Category
    FROM @TempTableA tta
    CROSS JOIN (SELECT DISTINCT ttb.Category FROM @TempTableB ttb) AS d
) AS t
LEFT OUTER JOIN @TempTableB ttb ON t.FinYearVal = ttb.FinYear AND ttb.Category = t.Category
ORDER BY t.Category, t.FinYearVal

If you have special Category table, query can be impruved by replacing cross join on distinct with cross join on this table 如果您有特殊的“类别”表,则可以通过将此表上的“交叉连接”替换为“交叉连接”来简化查询

Can you try this? 你可以试试这个吗?

select t1.category,t1.finyearval,coalesce(t2.amount,0) from
(
select t1.category,t2.finyearval from @TempTableB as t1 inner join @TempTableA as t2 on 1=1
) as t1 left join @TempTableB as t2 on t1.finyearval=t2.finyear and t1.Category=t2.Category

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

相关问题 postgres中的Sql:选择一个表中与其他和其他相关的每一行 - Sql in postgres: select every row in one table that is related to other and other 我想以特定顺序将表格的每一行与另一行连接起来 - i want to join every row of a table with another one in a specific order SQL将一个表两次连接到另一个表 - SQL join one table to other table twice 是否可以在一个行中将一个表中的SELECT与另一个表中的另一个SELECT连接起来? - Is it possible to join a SELECT from one table with another SELECT from other table in a single row? 如何使用LINQ2SQL将一个表中的每一行与另一表中的最新日期行连接在一起 - How to join every row from one table, with the latest dated row from another table, using LINQ2SQL 如何为另一个表的每一行联接到表? - How to Join to tables for each row of the other table? 表联接返回整个表中的每一行 - Table join returns every row from entire table 如何将 sql 表与另一个表连接起来,其中一个匹配的每个值重复另一个表中的行数? - How to join a sql table with another where each value in one that matches repeats the amount of row in the other one? 选择一个表行以连接到另一表的每一行 - Select one table row to join to each row from another table SQL在另一行的每一行上过滤一个表 - SQL filter one table on every row of another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM