繁体   English   中英

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]

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

 (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的结果。[位置代码] t1的结果也没有显示。

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

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

先谢谢...

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

问题在于您使用的逗号连接和 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]

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

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

暂无
暂无

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

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