2015-04-15 22 views
1

當我嘗試將第一個嵌套select語句除以列的計數時,查詢返回一個零。當我用「,」替換「/」時,我收到兩個不同的數字,所以返回值不應該爲零。這可能與數據集中存在零有關嗎?任何幫助,將不勝感激在select語句中分割時查詢返回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 count(iscoded) 
from ope.ope.vwerali 
    where iscoded=1 
     and [email protected] 
     and reportdate between @startdate and @enddate 
     and reportname like @reportname 
     and metasectionname like @metasectionname 
) 
/count(iscoded) 
from ope.ope.vwerali 
    where [email protected] 
     and reportdate between @startdate and @enddate 
     and reportname like @reportname 
     and metasectionname like @metasectionname 
+1

「我收到兩個不同的號碼」 ..你收到了什麼號碼?計數返回整數。如果不轉換爲其他數據類型,並且分母>分子,則結果爲0。例如:'select 1/2' – Dan

+1

像Dan說的那樣,但要更具體一點...將CAST中的分母(nullif(count(iscoded),0)包裹爲float)。 SQL Server處理整數除與常規數學不同。 Nullif將防止除以零錯誤 –

+0

嵌套的select語句返回2484和count(iscoded)返回3569.我試過將列轉換爲int,但查詢仍返回零。 – WSL346

回答

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 [email protected] 
     and reportdate between @startdate and @enddate 
     and reportname like @reportname 
     and metasectionname like @metasectionname 
+0

不幸的是,這仍然返回一個零。 – WSL346

+0

@ WSL346現在嘗試第一次它將它作爲int轉換,現在它應該返回小數 –

+0

我使用另一種方法解決了我的問題,但此答案也適用,並且更加緊湊。謝謝@ M.Ali – WSL346

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 [email protected] 
     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 [email protected] 
     and reportdate between @startdate and @enddate 
     and reportname like @reportname 
     and metasectionname like @metasectionname 
0

我知道了返回預期值我一直在尋找。信貸去凱文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 [email protected] 
     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 [email protected] 
     and reportdate between @startdate and @enddate 
     and reportname like @reportname 
     and metasectionname like @metasectionname