SQL 其中 2 个值一个为空

SQL Where 2 values one is empty

几个小时以来,我一直在努力编写一些 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]

这是第二个查询,有时 return 没有值。

 (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 服务器上为 SQl 使用 Toad。

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

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

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

中的所有原始记录

我已经更新了您的代码,因此它使用 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]