不替换任何结果

时间:2013-05-30 12:23:47

标签: sql tsql

我有这样的查询:

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 是没用的。

4 个答案:

答案 0 :(得分:2)

使用COALESCE。它返回第一个非null值。 E.g。

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

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

答案 1 :(得分:2)

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

这是一个带有表变量@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

答案 2 :(得分:0)

您可以尝试:

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

那应该做你想要的......

答案 3 :(得分:-3)

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

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