SQL其中2个值为空

时间:2015-06-10 13:22:52

标签: sql sql-server record

我现在正在努力解决一些SQL代码问题。我试图在一行中组合两个不同的值,但如果没有一个值(所以没有结果),则根本不会排。

更清楚一点:我有一个位置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。

2 个答案:

答案 0 :(得分:1)

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

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

以下内容应该是您正在寻找的内容:

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]

答案 1 :(得分:0)

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

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