没有找到记录的SQL TOP 1问题

时间:2012-03-01 08:27:15

标签: sql

希望你能解决我无法解决的SQL问题:

我有一个价目表文件,每个项目编号包含多个条目,如下所示:

  • 货号
  • 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

1 个答案:

答案 0 :(得分:0)

如果您请求的数据集为空,则“前1”将返回数据集中的第一行,然后前1将返回没有行且没有“随机数据”的空数据集。

如果您在查询中稍后需要数据或初始化变量并使用该数据,则应该检查返回的行数。