Getting two counts and then dividing them
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
Trying to divide two counts in SQL
Oracle
SQL Fiddle: http://sqlfiddle.com/#!4/34298/8
Method 1:
SELECT 1 - (COUNT(DISTINCT DID) * 100 / COUNT(DISTINCT ID))
FROM TGAMAZING
cross join DIRECTORS;
Method 2:
SELECT 1 -
(
(SELECT COUNT(DISTINCT DID) FROM TGAMAZING) * 100 /
(SELECT COUNT(DISTINCT ID) FROM DIRECTORS)
)
FROM DUAL;
SQL Server
SQL Fiddle: http://sqlfiddle.com/#!6/34298/3
Method 1
SELECT 1 - (COUNT(DISTINCT DID) * 100.0 / COUNT(DISTINCT ID))
FROM TGAMAZING
cross join DIRECTORS;
Method 2
SELECT 1 -
(
(SELECT COUNT(DISTINCT DID) FROM TGAMAZING) * 100.0 /
(SELECT COUNT(DISTINCT ID) FROM DIRECTORS)
)
How to divide count from one table by the count from the other one or other solution needed
number of users who logged in more than once divided by all users within that day
You can do this with two levels of aggregation: first count the logins per user and day, then compute the ratio of counts greater than 1.
select session_date, avg((cnt > 1)::int) retention_rate
from (
select date_trunc('day', session_time) session_date, count(*) cnt
from retention_cohort
group by session_date, device_id
) t
group by session_date
Getting a count of two different sets of rows in a table and then dividing them
This should work:
select
(select count(*) from tasks where completed = 1) /
(select count(*) from tasks where completed = 0)
from dual
Dividing two results of count with eachother in SQL
You could try following query:
SELECT COUNT(student.userid)::decimal / (SELECT COUNT(userid) FROM student)
FROM borrowed
CROSS JOIN student
WHERE student.userid = borrowed.userid
AND current_date > expiredate AND returndate IS NULL;
Divide the results of two select queries
You may use conditional aggregation and simple division in one query:
select
100.0 *
count(
case when ISOGEN_LINE_PROGRESS_ = 'C'
then 1
end
)
/
nullif(count(
case when PROJECT_NUMBER_ = 'PJ001234'
then 1
end
), 0) as rate_
FROM dbo.PID_Components_PROCESS_LINES
WHERE ISOGEN_LINE_PROGRESS_ = 'C'
or PROJECT_NUMBER_ = 'PJ001234'
Related Topics
How to Find Multiple Occurrence of Particular String and Fetch Value in SQL Server
How to Get the Month and Day With Leading 0'S in SQL (E.G. 9 => 09)
Sql Select Join: How to Prefix All Columns as 'Prefix.*'
Delete Rows With Date Older Than 30 Days With SQL Server Query
How to Change Column Order in a Table Using SQL Query in SQL Server 2005
How to Retrieve Records for Last 30 Minutes in Ms SQL
How to Parser a SQL Query to Pull Out the Column Names and Table Names
Update Table Column With Different Random Numbers
When to Use Single Quotes, Double Quotes, and Backticks in MySQL
SQL Select Only Rows with Max Value on a Column
How to Return Pivot Table Output in MySQL
Select First Row in Each GROUP BY Group
Simulating group_concat MySQL Function in Microsoft SQL Server 2005
Retrieving the Last Record in Each Group - MySQL
SQL Server Dynamic PIVOT Query
Fetch the Rows Which Have the Max Value for a Column for Each Distinct Value of Another Column