Calculation field using division in SQLite3

Solution for Calculation field using division in SQLite3
is Given Below:

I have a sqlite3 table with 2 fields of INT. (Passed and Failed)

I want to calculate % Passed so am using the following SQL

SELECT id,
       passed,
       failed,
       passed / (passed + failed )* 100 AS 'passedPC'
  FROM myData

if Passed is 23 and Failed is 3 I am getting 0 returned which is unexpected.

Why is this?

It’s because you are doing integer division and 23/26=0. You need to force it to do floating point division instead. The easiest way is to multiply one value with 1.0 like this:

SELECT id, passed, failed, ((passed * 1.0) / (passed + failed )) * 100 AS 'passedPC' 
FROM myData

Alternatively you could use the cast operator to force a a column to the real data type:

SELECT id, passed, failed, cast(passed as real) / (passed + failed ) * 100 AS 'passedPC' 
FROM myData;

Sample SQL Fiddle showing the difference.

Integer division truncates the result.

You might multiply before division and/or cast to a different datatype:

SELECT id, passed, failed, 
  100 * passed / (passed + failed ) AS 'passedPC' 
FROM myData

SELECT id, passed, failed, 
  100.00 * passed / (passed + failed ) AS 'passedPC' 
FROM myData