试图在SQL中划分两个计数

时间:2014-09-12 00:40:33

标签: sql oracle plsql

因此,我试图进行简单的划分,当然,SQL是超级合乎逻辑的,这使得它变得不可能。我想要做的是:

SELECT * FROM 
1 - ((SELECT COUNT(DISTINCT T.DID) FROM TGAMAZING T AS NUM) * 100 /
(SELECT COUNT(DISTINCT D.ID) FROM DIRECTORS D AS DENOM))

但我该怎么写这个SQL(Oracle)?

2 个答案:

答案 0 :(得分:4)

<强>的Oracle

SQL小提琴:http://sqlfiddle.com/#!4/34298/8

方法1:

SELECT 1 - (COUNT(DISTINCT DID) * 100 / COUNT(DISTINCT ID))
FROM TGAMAZING
cross join DIRECTORS;

方法2:

SELECT 1 - 
    (
        (SELECT COUNT(DISTINCT DID) FROM TGAMAZING) * 100 /
        (SELECT COUNT(DISTINCT ID) FROM DIRECTORS)
    )
FROM DUAL;

SQL Server

SQL小提琴:http://sqlfiddle.com/#!6/34298/3

方法1

SELECT 1 - (COUNT(DISTINCT DID) * 100.0 / COUNT(DISTINCT ID))
FROM TGAMAZING
cross join DIRECTORS;

方法2

SELECT 1 - 
    (
        (SELECT COUNT(DISTINCT DID) FROM TGAMAZING) * 100.0 /
        (SELECT COUNT(DISTINCT ID) FROM DIRECTORS)
    )

答案 1 :(得分:2)

将计算写为字段并使用&#34; FROM DUAL&#34;,因为您不想查询任何表格。

SELECT  
  1 - ((SELECT COUNT(DISTINCT T.DID) FROM TGAMAZING T) * 100 /
  (SELECT COUNT(DISTINCT D.ID) FROM DIRECTORS D))
  AS RESULT
FROM DUAL

参考:Selecting from the DUAL Table