SQLite does integer division. So, 1/2
is 0
rather than 0.5
.
So, the simplest method is to use 100.0
instead of 100
:
SELECT ((SELECT count(call) FROM table1) * 100.0 /
(SELECT count(pickup) FROM table2)
) as call_to_pickup_ratio
This, in turn, runs the ricks of a divide by zero. The solution for that is NULLIF()
:
SELECT ((SELECT count(call) FROM table1) * 100.0 /
(SELECT NULLIF(count(pickup), 0) FROM table2)
Finally, if call
and pickup
are never NULL
, then you can just use COUNT(*)
:
SELECT ((SELECT count(*) FROM table1) * 100.0 /
(SELECT NULLIF(count(*), 0) FROM table2)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…