在select语句中进行分割时,查询返回零

时间:2015-04-15 17:36:01

标签: sql sql-server

当我尝试将第一个嵌套的select语句除以列的计数时,查询返回零。当我用“,”替换“/”时,我收到两个不同的数字,因此返回值不应为零。这可能与数据集中的零有关吗?任何帮助将不胜感激

declare @hospitalfk int;
set @hospitalfk='1335'

declare @startdate date;
set @startdate='03/01/2014'

declare @enddate date;
set @enddate='02/28/2015'

declare @reportname varchar(50);
set @reportname='%Medicaid Billable Report%'

declare @metasectionname varchar(100);
set @metasectionname='Medicaid Primary'

select 
(
select count(iscoded)
from ope.ope.vwerali 
    where iscoded=1 
        and hospitalfk=@hospitalfk
        and reportdate between @startdate and @enddate
        and reportname like @reportname
        and metasectionname like @metasectionname
)
/count(iscoded)
from ope.ope.vwerali
    where hospitalfk=@hospitalfk
        and reportdate between @startdate and @enddate
        and reportname like @reportname
        and metasectionname like @metasectionname

3 个答案:

答案 0 :(得分:1)

不需要两个选择,只需使用Case语句进行计数。

此外,我认为你试图在除数中避免零0

日期参数也将日期值传递为ANSI日期,即YYYYMMDD

select count(CASE WHEN iscoded=1  THEN iscoded END) * 1.00
      / NULLIF(count(iscoded), 0) 
from ope.ope.vwerali 
    where  hospitalfk=@hospitalfk
        and reportdate between @startdate and @enddate
        and reportname like @reportname
        and metasectionname like @metasectionname

答案 1 :(得分:0)

declare @hospitalfk int;
set @hospitalfk='1335'

declare @startdate date;
set @startdate='03/01/2014'

declare @enddate date;
set @enddate='02/28/2015'

declare @reportname varchar(50);
set @reportname='%Medicaid Billable Report%'

declare @metasectionname varchar(100);
set @metasectionname='Medicaid Primary'

select 
(
    select CONVERT(NUMERIC(10,2),count(iscoded))
    from ope.ope.vwerali 
    where iscoded=1 
        and hospitalfk=@hospitalfk
        and reportdate between @startdate and @enddate
        and reportname like @reportname
        and metasectionname like @metasectionname
)
/CONVERT(NUMERIC(10,2),count(iscoded))
from ope.ope.vwerali
    where hospitalfk=@hospitalfk
        and reportdate between @startdate and @enddate
        and reportname like @reportname
        and metasectionname like @metasectionname

答案 2 :(得分:0)

我已经恢复了我想要的预期价值。感谢Kevin Suchlicki。我需要做的就是改变:

count(iscoded)

convert(float,nullif(count(iscoded),0))

更新了查询

declare @hospitalfk int;
set @hospitalfk='1335'

declare @startdate date;
set @startdate='03/01/2014'

declare @enddate date;
set @enddate='02/28/2015'

declare @reportname varchar(50);
set @reportname='%Medicaid Billable Report%'

declare @metasectionname varchar(100);
set @metasectionname='Medicaid Primary'

select 
(
select convert(float,nullif(count(iscoded),0))
from ope.ope.vwerali 
    where iscoded=1 and hospitalfk=@hospitalfk
        and reportdate between @startdate and @enddate
        and reportname like @reportname
        and metasectionname like @metasectionname
)
/convert(float,nullif(count(iscoded),0))
from ope.ope.vwerali
    where hospitalfk=@hospitalfk
        and reportdate between @startdate and @enddate
        and reportname like @reportname
        and metasectionname like @metasectionname