[英]Query returning a zero when dividing in the select statement
當我嘗試將第一個嵌套的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
不需要兩個選擇,只需使用Case語句進行計數即可。
此外,我認為你正試圖避免零0
的除數。
同樣對於日期參數,將日期值作為ANSI Date傳遞,即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
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
我得到了它返回想要的期望值。 歸功於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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.