Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting two counts and then dividing them

I am attempting to get two counts and then divide those two counts to get the ratio of the items I am counting. I saw this post here and tried that. I am getting an error in my results, no error message just incorrect number. I am using SQL-Server 2008

Here is my code:

-- INTERNAL PEPPER REPORT
--#####################################################################

-- VARIABLE DECLARATION AND INITIALIZATION
DECLARE @SD DATETIME
DECLARE @ED DATETIME

SET @SD = '2013-01-01'
SET @ED = '2013-03-31'

-- TABLE DECLARATION ##################################################
DECLARE @TABLE1 TABLE(NUMERATOR INT, DENOMINATOR INT, RATIO INT)
--#####################################################################

-- WHAT GETS INSERTED INTO TABLE 1
INSERT INTO @TABLE1
SELECT
A.NUM, A.DENOM, A.NUM/A.DENOM 

FROM
(
-- COLUMN SELECTION. TWO NUMBERS WILL REPRESENT A NUM AND A DENOM
SELECT 
    (SELECT COUNT(DRG_NO)
        FROM smsdss.BMH_PLM_PtAcct_V
        WHERE drg_no IN (061,062,063,064,065,066)
        AND Adm_Date BETWEEN @SD AND @ED
        AND PLM_PT_ACCT_TYPE = 'I')
        AS NUM,
    (SELECT COUNT(DRG_NO)
        FROM smsdss.BMH_PLM_PtAcct_V
        WHERE drg_no IN (061,062,063,064,065,066,067,068,069)
        AND Adm_Date BETWEEN @SD AND @ED
        AND Plm_Pt_Acct_Type = 'I')
        AS DENOM
)A

SELECT NUMERATOR, DENOMINATOR, RATIO
FROM @TABLE1

The counts get produced and displayed correctly, but for a ratio I get 0 and am not sure as to why I get this.

Thank You,

like image 770
MCP_infiltrator Avatar asked Jun 11 '13 18:06

MCP_infiltrator


People also ask

Which is faster count 1 or count (*)?

The simple answer is no – there is no difference at all. The COUNT(*) function counts the total rows in the table, including the NULL values. The semantics for COUNT(1) differ slightly; we'll discuss them later. However, the results for COUNT(*) and COUNT(1) are identical.

What does count 1 mean?

COUNT(*) counts all the rows including NULLs. COUNT(1) counts all the rows including NULLs. COUNT(column_name) counts all the rows but not NULLs. Hope this helps.


1 Answers

Use SELECT A.NUM, A.DENOM, cast(A.NUM as float)/cast(A.DENOM as float)

SQL Server consider that A.NUM / A.DENOM are int, because A.NUM and A.DENUM are int

like image 145
VahiD Avatar answered Sep 30 '22 03:09

VahiD