2012-03-01 105 views
0

希望你能幫助我解決這個SQL問題我無法解決1個問題:SQL TOP時沒有找到記錄

我有每個項目數的多個條目像這樣的價格列表文件:

  • 貨號
  • ValidFrom
  • ValidTo
  • 價格

現在,當我嘗試使用今天有效的價格生成列表時,我使用以下聲明來獲得體面的報告。沒有合適的價格時,我只有一個問題,即今天沒有記錄。在這種情況下,第一個TOP 1子查詢仍然返回一個(隨機?)值並銷燬我的報告。

我認爲TOP 1會一直按照定義返回一個值,或者是否有一種可以防止這種情況的技巧?

非常感謝您的幫助! (和請原諒德國方面在查詢中...)

SELECT 
    MITBAL.MBITNO as 'Artikel', MITMAS.MMITDS as 'Bezeichnung', 
    MITBAL.MBBUYE as 'Disponent', MITMAS.MMNEWE as 'Gewicht', 
    MITBAL.MBSUNO as 'Lieferant', MMITTY as 'Typ', 
    m9ucos as 'Std.-Kosten', idsunm as 'Lieferantenname', 
    iicucd as 'WSL', MBEOQT as 'EOQ', 

    (select count(*) 
    from RCE12_Staging.dbo.mpagrl 
    left outer join RCE12_Staging.dbo.mpagrh on aicono = ahcono 
              and aiagnb = ahagnb and aisuno = ahsuno 
    where aicono = 2 
    and aiobv1 = mbitno and aisagl = 20 
    and aisuno between '400000' and '599999' and aigrpi = 40 
    and aiuvdt >= CONVERT(char(8), GETDATE(), 112) and ahpast = '40') as 'Anz.Lief', 

    (select TOP 1 ajpupr 
    from RCE12_Staging.dbo.mpagrp 
    left outer join RCE12_Staging.dbo.mpagrh on ajcono = ahcono 
          and ajagnb = ahagnb and ajsuno = ahsuno 
    left outer join RCE12_Staging.dbo.mpagrl on ajcono = aicono 
          and ajagnb = aiagnb and ajsuno = aisuno 
    where ajcono = 2 
    and ajobv1 = mbitno and ajsuno = mbsuno 
    and ajmapr = 1 and ajgrpi = 40 
    and ajfvdt < CONVERT(char(8), GETDATE(), 112) and ahpast = '40' 
    and ahfvdt < CONVERT(char(8), GETDATE(), 112) 
    and ahuvdt >= CONVERT(char(8), GETDATE(), 112) 
    and aisagl = 20 and aigrpi = 40 
    and aifvdt < CONVERT(char(8), GETDATE(), 112) 
    and aiuvdt >= CONVERT(char(8), GETDATE(), 112) 
    order by ajfvdt desc) as 'EK-Preis', 

    (select TOP 1 ajfrqt 
    from RCE12_Staging.dbo.mpagrp 
    left outer join RCE12_Staging.dbo.mpagrh on ajcono = ahcono 
          and ajagnb = ahagnb and ajsuno = ahsuno 
    left outer join RCE12_Staging.dbo.mpagrl on ajcono = aicono 
          and ajagnb = aiagnb and ajsuno = aisuno 
    where ajcono = 2 and ajobv1 = mbitno 
    and ajsuno = mbsuno and ajmapr = 1 and ajgrpi = 40 
    and ajfvdt < CONVERT(char(8), GETDATE(), 112) and ahpast = '40' 
    and ahfvdt < CONVERT(char(8), GETDATE(), 112) 
    and ahuvdt >= CONVERT(char(8), GETDATE(), 112) and aisagl = 20 
    and aigrpi = 40 
    and aifvdt < CONVERT(char(8), GETDATE(), 112) 
    and aiuvdt >= CONVERT(char(8), GETDATE(), 112) 
    order by ajfvdt desc) as 'EK-Menge' 

FROM 
    RCE12_Staging.dbo.MITBAL MITBAL 
left outer join 
    RCE12_Staging.dbo.MITFAC MITFAC ON mbcono = m9cono and mbitno = m9itno and m9faci = 'DFP' 
left outer join 
    RCE12_Staging.dbo.MITMAS MITMAS ON mbcono = mmcono and mbitno = mmitno 
left outer join 
    RCE12_Staging.dbo.CIDMAS CIDMAS ON mbcono = idcono and mbsuno = idsuno 
left outer join 
    RCE12_Staging.dbo.CIDVEN CIDVEN ON mbcono = iicono and mbsuno = iisuno 

WHERE 
    MITBAL.MBCONO = 2 
    AND MITBAL.MBWHLO = '200' AND MBSTAT = '20' AND MBPUIT = '2' 
    and MBITNO like '479200222%' 

ORDER BY MBITNO 
+2

歡迎使用StackOverflow:如果您發佈代碼,XML或數據樣本,請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼示例」按鈕(「{}」)格式和語法突出顯示它! – 2012-03-01 08:28:58

+0

如果您運行聲明但刪除了「TOP 1」,您會收到結果嗎?如果是這樣的話,問題出在你的查詢上,而不是「TOP 1」。 – Fermin 2012-03-01 08:31:48

+2

另請嘗試**將問題簡化爲顯示您遇到的問題的最小查詢。我們不需要所有那些WHERE條件和一個'SELECT TOP 1 ...'子查詢的十個示例 - 將它減少到**只是顯示**問題 - 不要轉儲這樣一個巨大的,難以閱讀的查詢關於我們...... – 2012-03-01 08:33:34

回答

0

「頂層1」會從數據集中返回的第一行,如果您所請求的數據集爲空,則頂部1將返回一個空集沒有行,也沒有「隨機數據」。

如果您需要稍後查詢數據或初始化變量並使用該數據,您應該檢查返回的行數。

+0

感謝您的意見,您指出我的方向正確。問題解決了。 – 2012-03-01 10:13:45

相關問題