Percentage calculation in SQLite

Here's the request:

select
(
SELECT     COUNT(*)
FROM         Sending s
WHERE     (ConfID = 1) AND (Status = 1)
)
/
(
 (
 SELECT     COUNT(*)
 FROM         Numbers
 WHERE     (ConfID = 1)
 )
 /
 100
)

I want to calculate interest. For example, if the request

SELECT     COUNT(*)
     FROM         Numbers
     WHERE     (ConfID = 1)

gives 100 and the other

 SELECT     COUNT(*)
    FROM         Sending s
    WHERE     (ConfID = 1) AND (Status = 1)

Results 50 of the query result should return 50, which means 50%. But the first query results with 2 and the second returns 10000, the query result returns 0. I think I should somehow determine the number of a floating point or so on.

Thank!

+5
source share
2 answers

Try to divide by 100.0instead 100. This will force the floating point data type:

select
(
SELECT     COUNT(*)
FROM         Sending s
WHERE     (ConfID = 1) AND (Status = 1)
)
/
(
 (
 SELECT     COUNT(*)
 FROM         Numbers
 WHERE     (ConfID = 1)
 )
 /
 100.0
)
+7
source

Try:

SELECT SUM(CASE status WHEN 1 THEN 100 ELSE 0 END) / COUNT(*)
  FROM numbers
 WHERE confid = 1
0
source

All Articles