【问题标题】:Replace no result替换没有结果
【发布时间】:2013-05-30 12:23:46
【问题描述】:

我有一个这样的查询:

SELECT TV.Descrizione as TipoVers, 
       sum(ImportoVersamento) as ImpTot, 
       count(*) as N,
       month(DataAllibramento) as Mese
FROM PROC_Versamento V
left outer join dbo.PROC_TipoVersamento TV
    on V.IDTipoVersamento = TV.IDTipoVersamento
inner join dbo.PROC_PraticaRiscossione PR 
    on V.IDPraticaRiscossioneAssociata = PR.IDPratica
inner join dbo.DA_Avviso A
    on PR.IDDatiAvviso = A.IDAvviso
where DataAllibramento between '2012-09-08' and '2012-09-17' and  A.IDFornitura = 4
group by V.IDTipoVersamento,month(DataAllibramento),TV.Descrizione
order by V.IDTipoVersamento,month(DataAllibramento)

这个查询必须总是返回一些东西。如果没有结果产生一个

0 0 0 0

必须返回行。我怎样才能做到这一点。对每个选定的字段使用 isnull 没有用。

【问题讨论】:

  • Kinfly 提供完整的查询。
  • @Luv 全部代码现报...

标签: sql tsql


【解决方案1】:

使用具有一行的派生表,并对您的其他表/查询执行外部应用。

这是一个示例,其中表变量 @T 代替了您的真实表。

declare @T table
(
  ID int,
  Grp int
)

select isnull(Q.MaxID, 0) as MaxID,
       isnull(Q.C, 0) as C
from (select 1) as T(X)
  outer apply (
              -- Your query goes here
              select max(ID) as MaxID,
                     count(*) as C
              from @T
              group by Grp
              ) as Q
order by Q.C -- order by goes to the outer query

这将确保您在输出中始终至少有一行。

使用您的查询类似这样。

select isnull(Q.TipoVers, '0') as TipoVers, 
       isnull(Q.ImpTot, 0) as ImpTot, 
       isnull(Q.N, 0) as N,
       isnull(Q.Mese, 0) as Mese
from (select 1) as T(X)
  outer apply (
              SELECT TV.Descrizione as TipoVers, 
                     sum(ImportoVersamento) as ImpTot, 
                     count(*) as N,
                     month(DataAllibramento) as Mese,
                     V.IDTipoVersamento
              FROM PROC_Versamento V
              left outer join dbo.PROC_TipoVersamento TV
                  on V.IDTipoVersamento = TV.IDTipoVersamento
              inner join dbo.PROC_PraticaRiscossione PR 
                  on V.IDPraticaRiscossioneAssociata = PR.IDPratica
              inner join dbo.DA_Avviso A
                  on PR.IDDatiAvviso = A.IDAvviso
              where DataAllibramento between '2012-09-08' and '2012-09-17' and  A.IDFornitura = 4
              group by V.IDTipoVersamento,month(DataAllibramento),TV.Descrizione
              ) as Q
order by Q.IDTipoVersamento, Q.Mese

【讨论】:

  • Msg 156, Level 15, State 1, Line 5 关键字“values”附近的语法不正确。消息 156,级别 15,状态 1,第 21 行关键字“as”附近的语法不正确。
  • @marianoc84 您可能使用的是 SQL Server 2005。尝试更新版本的答案。
  • 完美!我只是用 isnull 替换了 nullif! ;)
  • @marianoc84 哈哈。这肯定会改善问题:)
  • @marianoc84 不太明白。行上应该有什么值?为什么你得到多少行很重要?作为一个新问题,您可能应该问这个问题,您可以在其中提供一些示例数据和预期输出的表结构。您甚至可以设置一个SQL Fiddle 来帮助回答者。
【解决方案2】:

使用COALESCE。它返回第一个非空值。例如

SELECT COALESCE(TV.Desc, 0)...

如果 TV.DESC 为 NULL,则返回 0。

【讨论】:

  • 它不起作用。我们确定没有结果与 NULL 相同吗?
  • 嗯,这取决于数据库中的数据。您确实提到了使用 isnull,所以我假设您正在检查一些非空值。您具体考虑什么“没有结果”?一个空字符串?
【解决方案3】:

你可以试试:

with    dat as (select    TV.[Desc] as TipyDesc, sum(Import) as ToImp, count(*) as N, month(Date) as Mounth
                from        /*DATA SOURCE HERE*/ as TV
                group by    [Desc], month(Date))
    select    [TipyDesc], ToImp, N, Mounth from    dat
    union all
    select    '0', 0, 0, 0 where    (select count (*) from dat)=0

这应该做你想做的......

【讨论】:

  • 附言。我猜“无结果”是指没有返回任何行。
【解决方案4】:

如果可以在具有数据的结果集中包含“0 0 0 0”行,则可以使用联合:

SELECT TV.Desc as TipyDesc, 
   sum(Import) as TotImp, 
   count(*) as N,
   month(Date) as Mounth
   ...
UNION
SELECT
    0,0,0,0

根据数据库,第二个 SELECT 可能需要 FROM。在 Oracle 中,这将是“FROM DUAL”。对于 MySQL,不需要 FROM

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2020-08-31
    • 2013-11-29
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多