简体   繁体   English

SQL 其中 2 个值之一为空

[英]SQL Where 2 values one is empty

I'm struggling whit some SQL code for some hours now.我现在正在为一些 SQL 代码苦苦挣扎了几个小时。 I'm trying to combine 2 different values in one row, but if one value is not there (so no result) there will not row at all.我试图在一行中组合 2 个不同的值,但如果一个值不存在(因此没有结果),则根本不会行。

To be more clear: I have a Location whit 2 different values, those are coming from two queries.更清楚地说:我有一个带有 2 个不同值的位置,这些值来自两个查询。 That is working fine, but sometime the second query give no results (can happen, is not bad), but than also the first value is not shown.这工作正常,但有时第二个查询没有结果(可能发生,还不错),但第一个值也未显示。

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]

It is the second query that sometimes does not return a value.这是有时不返回值的第二个查询。

 (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

But that when it comes to the end and there is no result of t2.[Location code] the result of t1 is also not shown.但是到最后没有t2的结果。[位置代码] t1的结果也没有显示。

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

I want that t2 gets a value of zero when there is no result.当没有结果时,我希望 t2 的值为零。 I tried isnull and the coalesec option but I was not able to get a decent result.我尝试了 isnull 和 coalesec 选项,但无法获得不错的结果。 It is just not there or I get error messages.它不存在或者我收到错误消息。

Thank in advanced...先谢谢...

Using Toad for SQl on a 2012 MSSQL server.在 2012 MSSQL 服务器上使用 Toad for SQl。

The problem is that the comma join and where clause you're using makes the join an inner join (Thanks to the comment from Ed B for adding details to this).问题在于您使用的逗号连接和 where 子句使连接成为内部连接(感谢Ed B的评论为此添加了详细信息)。 In an inner join, only matching records are shown.在内部联接中,仅显示匹配的记录。 Since there are no records in t2, nothing is matching in t1, and no records are returned.由于 t2 中没有记录,因此 t1 中没有任何匹配,并且没有返回任何记录。 You're looking for a LEFT join, which will join any matching records from the 2nd table to the returned records from the 1st table.您正在寻找一个 LEFT 连接,它将第二个表中的任何匹配记录连接到第一个表中返回的记录。 If nothing is in the 2nd table, you still get all of the original records from the 1st table.如果第二个表中没有任何内容,您仍然可以从第一个表中获得所有原始记录。

I've updated your code so it uses a LEFT join, does the join in an ON statement instead of a where, and uses COALESCE to show 0 instead of NULL for records that don't match.我已经更新了您的代码,因此它使用 LEFT 连接,在 ON 语句而不是 where 中进行连接,并使用 COALESCE 为不匹配的记录显示 0 而不是 NULL。

The following should get what you're looking for:以下应该得到您正在寻找的内容:

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]

You should use a better syntax in your statements.您应该在语句中使用更好的语法。 Use a join instead of select from 2 comma seperated tables.使用连接而不是从 2 个逗号分隔的表中选择。 Then you'll see that you'll need a left join.然后你会看到你需要一个左连接。

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