【问题标题】:SQL Where 2 values one is emptySQL 其中 2 个值一个为空
【发布时间】:2015-08-25 18:48:30
【问题描述】:

我正在为一些 SQL 代码苦苦挣扎几个小时。我正在尝试将 2 个不同的值组合在一行中,但如果一个值不存在(所以没有结果),则根本不会出现行。

更清楚一点:我有一个包含 2 个不同值的位置,它们来自两个查询。这工作正常,但有时第二个查询没有结果(可能发生,还不错),但也没有显示第一个值。

Declare @Start datetime,
        @Ende datetime; 
SET @Start = '01.04.2015';
SET @Ende = '30.04.2015';

SELECT t1.[Location Code], CAST(t1.Umsatz as DECIMAL(18,2))as Umsatz , CAST(t2.Ersatznachweis as DECIMAL(18,2)) as Ersatznachweis
FROM (
SELECT [Location Code],  SUM(WareBrutto) AS Umsatz

FROM (SELECT  DISTINCT [Location Code], [Document No_] , WareBrutto from [Item Ledger Entry] 
      WHERE [Location Code] > '0000' and [Location Code] < '0040' and [Document Date] >= @Start and [Document Date] <= @Ende) t
GROUP BY [Location Code]) as t1,

(select [Location Code], sum([Quantity]*Bruttopreis) as Ersatznachweis  from [Item Ledger Entry] 
where [Location Code] > '0000' and [Location Code] < '0040' and [Item No_] not IN ('00009000','00009900','00009906') and Gutschrift = '1' and [Document Date] >= @Start and [Document Date] <= @Ende
Group By [Location Code]) as t2

where t1.[Location Code] = t2.[Location Code]
order by t1.[Location Code]

这是有时不返回值的第二个查询。

 (select [Location Code], sum([Quantity]*Bruttopreis) as Ersatznachweis  from [Item Ledger Entry] 
    where [Location Code] > '0000' and [Location Code] < '0040' and [Item No_] not IN ('00009000','00009900','00009906') and Gutschrift = '1' and [Document Date] >= @Start and [Document Date] <= @Ende
    Group By [Location Code]) as t2

但是到最后没有t2的结果。[Location code] t1的结果也没有显示出来。

where t1.[Location Code] = t2.[Location Code]

我希望 t2 在没有结果时获得零值。我尝试了 isnull 和 coalesec 选项,但我无法获得不错的结果。它不在那里,或者我收到错误消息。

在此感谢...

在 2012 MSSQL 服务器上使用 Toad for SQl。

【问题讨论】:

  • from 子句中删除逗号并使用明确的JOIN 语法。这样您就可以轻松地发现并解决问题。

标签: sql sql-server record


【解决方案1】:

您应该在语句中使用更好的语法。使用联接而不是从 2 个逗号分隔的表中选择。 然后你会看到你需要一个左连接。

SELECT ... AS t1
LEFT JOIN 
(SELECT ...) AS t2 ON t1.[Location Code] = t2.[Location Code]
...

【讨论】:

    【解决方案2】:

    问题在于您使用的逗号连接和 where 子句使连接成为内部连接(感谢 Ed B 的评论添加了详细信息)。在内部联接中,仅显示匹配的记录。由于 t2 中没有记录,因此 t1 中没有任何匹配项,因此不返回任何记录。您正在寻找一个 LEFT 联接,它将第二个表中的任何匹配记录连接到第一个表中返回的记录。如果第二张表中没有任何内容,您仍然可以从第一张表中获取所有原始记录。

    我更新了您的代码,使其使用 LEFT 连接,在 ON 语句中而不是 where 中进行连接,并使用 COALESCE 为不匹配的记录显示 0 而不是 NULL。

    以下内容应该可以满足您的需求:

    Declare @Start datetime,
            @Ende datetime; 
    SET @Start = '01.04.2015';
    SET @Ende = '30.04.2015';
    
    SELECT t1.[Location Code], CAST(t1.Umsatz as DECIMAL(18,2))as Umsatz , CAST(COALESCE(t2.Ersatznachweis, 0) as DECIMAL(18,2)) as Ersatznachweis
    FROM (
    SELECT [Location Code],  SUM(WareBrutto) AS Umsatz
    
    FROM (SELECT  DISTINCT [Location Code], [Document No_] , WareBrutto from [Item Ledger Entry] 
          WHERE [Location Code] > '0000' and [Location Code] < '0040' and [Document Date] >= @Start and [Document Date] <= @Ende) t
    GROUP BY [Location Code]) as t1
    
    LEFT JOIN (select [Location Code], sum([Quantity]*Bruttopreis) as Ersatznachweis  from [Item Ledger Entry] 
    where [Location Code] > '0000' and [Location Code] < '0040' and [Item No_] not IN ('00009000','00009900','00009906') and Gutschrift = '1' and [Document Date] >= @Start and [Document Date] <= @Ende
    Group By [Location Code]) as t2 ON t1.[Location Code] = t2.[Location Code]
    order by t1.[Location Code]
    

    【讨论】:

    • OP 应该知道“逗号连接”实际上是CARTESIAN JOINCROSS JOIN,其中逗号一侧的每条记录都与另一侧的每条记录相关。在这种情况下,只有WHERE 子句使它有效地成为INNER JOIN。避免使用这种JOIN 语法是一种很好的做法,除非你真的想要一个笛卡尔积,而是改用{[INNER] | {LEFT | RIGHT} [OUTER]} JOIN &lt;table&gt; ON &lt;condition&gt; 语法,它具有通过保持连接和过滤条件分开来提高可读性的额外优势。
    • 谢谢,比一个工作。现在我将尝试向它添加第三个查询。这会以同样的方式工作吗?
    • 取决于使用的连接语句。如果您对第一个表 (t1.col = t3.col) 进行左连接,则将显示来自第三个表的匹配记录。如果您对第二张表进行连接 (t2.col = t3.col),则仅当第二张表与第一张表匹配时,它才会显示第三张表中的匹配记录。在第一个示例中,t1.col = t3.col,这是一个新连接,即使它是同一查询的一部分,听起来可能就是您要查找的内容。这是一个可能有帮助的连接链接:technet.microsoft.com/en-us/library/ms191472(v=sql.105).aspx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多