Trying to divide two counts in SQL - sql

so I'm trying to do simple division and of course SQL, being super logical that it is, makes it impossible. What I am trying to do is this:
SELECT * FROM
1 - ((SELECT COUNT(DISTINCT T.DID) FROM TGAMAZING T AS NUM) * 100 /
(SELECT COUNT(DISTINCT D.ID) FROM DIRECTORS D AS DENOM))
but how would I write this SQL (Oracle)?

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)
)

Write the calculation as it were fields and use "FROM DUAL", as you don't want to query any of your tables.
SELECT
1 - ((SELECT COUNT(DISTINCT T.DID) FROM TGAMAZING T) * 100 /
(SELECT COUNT(DISTINCT D.ID) FROM DIRECTORS D))
AS RESULT
FROM DUAL
Reference: Selecting from the DUAL Table.

Related

How to divide the count() of two seperate queries in DB2

So I have
select count(*) from ( "query1")
select count(*) from ( "query2")
I want to divide the two and get the floating point result.
I was told to use something like this
SELECT (COUNT(smtg) * 1.0) / COUNT(smtg)
But Im not sure
You can just do:
select q1.cnt * 1.0 / q2.cnt
from (select count(*) as cnt from ( "query1") ) q1 cross join
(select count(*) as cnt from ( "query2") ) q2;
Or, if you prefer:
select ( (select count(*) from ( "query1")) * 1.0 /
(select count(*) from ( "query2"))
)
from sysibm.sysdummy1;
other solution (be carefull to not divide by 0)
with
query1 as (select count(*) as nb1 from ( "query1")),
query2 as (select count(*) as nb2 from ( "query2"))
select case when nb2=0 then null else nb1* 1.0/nb2 end as Result
from query1, query2

How can i divide two expressions in Microsoft SQL Server

How can i divide two expressions in Microsoft SQL Server which give Numbers as a Result? I tried with a /, but this didn't work
SELECT COUNT(*) FROM A
SELECT COUNT(*) FROM B WHERE c = 1
I tried with / but this didnt work
SELECT COUNT(*)
FROM A/SELECT COUNT(*) FROM B WHERE c = 1
It might depend on RDBMS, this works in sql server.
SELECT ACounts/ CAST(BCounts AS Decimal(29,20))
FROM (SELECT COUNT(*) AS ACounts FROM A) AS suba
, (SELECT COUNT(*) AS BCounts FROM B WHERE c = 1) AS subb
Note that I cast the BCounts to a decimal of arbitrary size otherwise it will round off to the nearest whole number. Ignore that part if that is what you wanted.
This also works
SELECT (SELECT COUNT(*) AS ACounts FROM A)/ CAST((SELECT COUNT(*) AS BCounts FROM B WHERE c = 1) AS Decimal(29,20))
Use subqueries:
SELECT (a.countA / b.countB) As divisionResult
FROM
(SELECT COUNT(*) AS countA FROM A) a,
(SELECT COUNT(*) AS countB FROM B WHERE c = 1) b
;

How can I get the count of multiple columns in SQL

Say I had two tables in SQL. Now I would like to get the quotient of the count of table 1 and count of table 2. How can I do that?
In Short:
(# of rows in table 1) / (# of rows in table 2)
EDIT:
This is what I tried:
SELECT COUNT(t1.a) / COUNT(t2.a)
FROM table1 t1, table2 t2
Here's one way to get the result:
SELECT c1.cnt / c2.cnt AS q
FROM ( SELECT COUNT(1) AS cnt
FROM table1
) c1
CROSS
JOIN ( SELECT COUNT(1) AS cnt
FROM table2
) c2
Another way to get an equivalent result:
SELECT (SELECT COUNT(1) FROM table1) / (SELECT COUNT(1) FROM table2) AS q
I would prefer the first query if I also needed to return the counts from the tables as separate columns in the resultset, for example:
SELECT c1.cnt AS table1_count
, c2.cnt AS table2_count
, c1.cnt / c2.cnt AS q
FROM ...
Try this:
SELECT COUNT(table1.column) as 'Table 1 Count'
,COUNT(table2.column) as 'Table 2 Count'
,COUNT(table1.column) / COUNT(table2.column) as 'Quotient'
FROM table1, table2
with
Ctable1 as
(select count(*) as num1 from table1),
Ctable2 as
(select count(*) as num2 from table2)
select num1 / num2 as quotient
from Ctable1,Ctable2
Remember:
When you count column, rows with "NULL" data will NOT count. (If you use Oracle, you can use count(a.*)
Int division in sql like most languages, returns int. (5/2 = 2 and not 2.5).

SQL-how to find the percentage

I have a table with following values id and amt
id amt amt%
1 500 16.7
2 600 20
3 900 30
4 1000 33.3
-----------------------------
3000 100%
I have to find the amt% for id=1 by dividing amt for id=1/sum(amt for all the ids).
How to write query for this in sql?
select id, amt, 100.0 * amt / (select sum(amt) from tab) as amt_percent
from tab
Mind 100.0, .0 is quite important it will "force" conversion ints to floats.
Added after comment:
select t.id, t.amt, 100.0 * t.amt / p.s as amt_percent
from tab t
join (
select id, sum(amt) as s
from tab
group by id
) p on p.id = t.id
and alternative way:
select id, amt,
100.0 * amt / (
select sum(amt)
from tab t1
where t1.id = t.id
) as amt_percent
from tab t
I prefer joining subquery - the first one.
You can try something like this:
SELECT
id, amt,
amt / (SELECT SUM(amt) FROM tabl) * 100 as amtperc
FROM tabl
select id, amt, 100*amt/(select NULLIF(sum(amt),0) from tbl) as "amt%"
from tbl
or maybe so
select t.id, t.amt, 100*amt/sm.s as "amt%"
from tbl t, (select NULLIF(sum(amt),0) as s from tbl) sm
edit check for dividing by zero
Assuming you are using Oracle (as in your previous question), try:
select id, amt, 100 * amt / sum(amt) over () amtperc
from your_table
select id, amt, 100 * amt/(case when (select sum(amt)as ss from tablename)>0 then
(select sum(amt)as ss from tablename) else 1 end) as [amt%] from tablename

simple sql query, combine results and divide

I'm trying to get 2 counts from 2 tables and work out the percentage like for a MySQL db:
select field_one, count(*) as COUNT_ONE from table1 group by field_one;
select other_field,count(*) as COUNT_TWO from table2 group by other_field;
I want to combine the results and have FINAL_COUNT=(COUNT_ONE/COUNT_TWO) * 100 for percentage ?
quick and dirty:
select (a.count_one / b.count_two) * 100 as final_count from
(select field_one, count(*) as count_one from table1 group by field_one) a,
(select field_two, count(*) as count_two from table2 group by field_two) b
where a.field_one = b.field_two
select sum(((QUERY FROM TABLE 1) / (QUERY FROM TABLE 2)) * 100) as percentage