0

このコードの理由を教えてください:

SELECT t.prob FROM 
(SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els) AS e2) AS t
 UNION ALL (SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els2 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els2) AS e2)
 UNION ALL (SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els3 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els3) AS e2);

「prob」の合計ではなく、これと同じ出力を生成しますか?

SELECT SUM(t.prob) FROM 
(SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els) AS e2) AS t
 UNION ALL (SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els2 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els2) AS e2)
 UNION ALL (SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els3 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els3) AS e2);

(コードは基本的に、3 つのテーブル els、els2、els3 のそれぞれに値を 1 つだけ含む列 prob を作成し、それらの 3 つを 1 つの列に結合して、そこから 3 つの要素の合計を求めます)

この別のコードを思いつきました。それは機能し、より明確なステートメントであるため、気にしないでください:

SELECT SUM(t.evaled/IFNULL(NULLIF(t.total,0),1)) as sumatory FROM
(SELECT evaled,total FROM
 (SELECT COUNT(*) AS evaled FROM els WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els) AS e2
UNION ALL SELECT * FROM
 (SELECT COUNT(*) AS evaled FROM els2 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els2) AS e2
UNION ALL SELECT * FROM
 (SELECT COUNT(*) AS evaled FROM els3 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els3) AS e2) as t;
4

1 に答える 1

1

あなたのソリューションで何がうまくいかないのかわかりませんが、私はあなたが達成しようとしていることを理解していると思います. このアプローチのようなものはどうですか:

SELECT 
 (SELECT COUNT(*)/(SELECT COUNT(*) FROM els) AS evaled FROM els WHERE evals=0)
 +
 (SELECT COUNT(*)/(SELECT COUNT(*) FROM els2) AS evaled FROM els2 WHERE evals=0) 
 +
 (SELECT COUNT(*)/(SELECT COUNT(*) FROM els3) AS evaled FROM els3 WHERE evals=0)
 /3

編集:そして、平均が必要な場合は、最後に が必要だと思います/3..

于 2012-04-15T13:10:07.713 に答える