当一个表没有行时,查询在连接时获取行

时间:2014-04-02 16:53:48

标签: sql sql-server-2008

我的SQL查询存在一些问题。请指教。任何帮助表示赞赏!

 CREATE TABLE #Table1
(
    Id  BIGINT,
    Name    VARCHAR(100),
    Amount  DECIMAL
)

CREATE TABLE #Table2
(
    Id  BIGINT,
    Name    VARCHAR(100),
    Amount  DECIMAL
)

        INSERT INTO #Table1
        SELECT Id, Name, ISNULL(dt.Amount, 0.00)
        FROM ConfigTable ct
        LEFT JOIN DataTable1 dt
        ON ct.Id = dt.ConfigId

        INSERT INTO #Table2
        SELECT Id, Name, ISNULL(dt.Amount, 0.00)
        FROM ConfigTable ct
        LEFT JOIN DataTable2 dt
        ON ct.Id = dt.ConfigId


        SELECT SUM(ISNULL(A.DataTable1Amount1, 0.0)) AS DataTable1Amount1,
               SUM(ISNULL(A.DataTable2Amount1, 0.0)) AS DataTable2Amount1,
               SUM(ISNULL(A.DataTable1Amount2, 0.0)) AS DataTable1Amount2,
               SUM(ISNULL(A.DataTable2Amount2, 0.0)) AS DataTable2Amount2,
               SUM(ISNULL(A.DataTable1Amount3, 0.0)) AS DataTable1Amount3,
               SUM(ISNULL(A.DataTable2Amount3, 0.0)) AS DataTable2Amount3
        FROM
        (
            SELECT CASE t1.Name WHEN 'ConfigName 1' THEN t1.Amount ELSE 0.00 END AS DataTable1Amount1,
               CASE t1.Name WHEN 'ConfigName 2' THEN t1.Amount ELSE 0.00 END AS DataTable1Amount2,
               CASE t1.Name WHEN 'ConfigName 3' THEN t1.Amount ELSE 0.00 END AS DataTable1Amount3,
               CASE t2.Name WHEN 'ConfigName 1' THEN t2.Amount ELSE 0.00 END AS DataTable2Amount1,
               CASE t2.Name WHEN 'ConfigName 2' THEN t2.Amount ELSE 0.00 END AS DataTable2Amount2,
               CASE t2.Name WHEN 'ConfigName 3' THEN t2.Amount ELSE 0.00 END AS DataTable2Amount3
            FROM  #Table1 t1, #Table2 t2
            WHERE t1.Id = t2.Id
        )A

示例数据:

Config表是一个包含以下数据的静态表:

Id, Name
1, ConfigName 1
2, ConfigName 2
3, ConfigName 3

DataTable1可能有也可能没有与Config表Id对应的行。如果它有行,则数据将采用以下格式:

DataTable1Id, ConfigId, Amount
1, 1, 10
2, 2, 20

DataTable2也可能有也可能没有与Config表Id对应的行。如果它有行,则数据将采用以下格式:

DataTable2Id, ConfigId, Amount
1, 1, 10
2, 2, 20

对于上述情况,预期输出将仅返回所有金额的一行。

DataTable1Amount1, DataTable2Amount1, DataTable1Amount2, DataTable2Amount2, DataTable1Amount3, DataTable2Amount3
10, 20, 0, 10, 20, 0

但是如果DataTable1或DataTable2没有任何与ConfigId对应的行,那么最终输出应该显示为0。

DataTable1Id, ConfigId, Amount
No rows

DataTable2Id, ConfigId, Amount
1, 1, 10
2, 2, 20

预期产出:

DataTable1Amount1, DataTable2Amount1, DataTable1Amount2, DataTable2Amount2, DataTable1Amount3, DataTable2Amount3
0, 0, 0, 10, 20, 0

当前输出:

DataTable1Amount1, DataTable2Amount1, DataTable1Amount2, DataTable2Amount2, DataTable1Amount3, DataTable2Amount3

不返回任何行。

如果我需要更具体,请告诉我。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我的问题已通过将查询中的过滤条件从WHERE子句移动到ON 来解决。感谢this发帖!

我正在查看联接,并且没有在我的示例查询中发布过滤器。但实际问题是当我在WHERE子句中使用第二个表中的过滤器时,当我对Join的ON上的第二个数据使用过滤器时,它不返回任何行Vs,将返回表格中第一个表的所有行。

Old query:

SELECT Id, Name, ISNULL(dt.Amount, 0.00)
        FROM ConfigTable ct
        LEFT JOIN DataTable1 dt
        ON ct.Id = dt.ConfigId
**WHERE dt.FilterColumn = c**

New query:

SELECT Id, Name, ISNULL(dt.Amount, 0.00)
        FROM ConfigTable ct
        LEFT JOIN DataTable1 dt
        ON ct.Id = dt.ConfigId **AND dt.FilterColumn = c**