Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute percent in SQLite

Tags:

sql

sqlite

Here's the query:

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

I want to compute percent. For instance, if query

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

gives 100 and the another one

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

results 50 the result query should return 50, which means 50%. But is first query results with 2 and second returns 10000 the result query returns 0. I think I should define somehow to return float number or so on.

Thanks!

like image 438
kseen Avatar asked Oct 05 '11 13:10

kseen


1 Answers

Try dividing by 100.0 instead of 100. That will force a cast to 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
)
like image 66
Adriano Carneiro Avatar answered Oct 06 '22 02:10

Adriano Carneiro