簡體   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