0

派生列を別のプロシージャの入力として使用することで、このクエリを簡略化できますか?

$query = "SELECT   table1.id,
         COUNT(DISTINCT arr1.element) AS Arr1,
         COUNT(DISTINCT arr2.element) AS Arr2,
         COUNT(DISTINCT arr1.element) +
         COUNT(DISTINCT arr2.element) AS tot,
(COUNT(DISTINCT arr1.element)/(COUNT(DISTINCT arr1.element)+COUNT(DISTINCT arr2.element)))*col3 AS c31,
(COUNT(DISTINCT arr2.element)/(COUNT(DISTINCT arr1.element)+COUNT(DISTINCT arr2.element)))*col3 AS c32
FROM table1 
WHERE col2 = 'x'" 

このようなもの

$query = "SELECT   table1.id,
         COUNT(DISTINCT arr1.element) AS Arr1,
         COUNT(DISTINCT arr2.element) AS Arr2,
         COUNT(DISTINCT arr1.element) +
         COUNT(DISTINCT arr2.element) AS tot,
        (Arr1/tot)*col3 AS c31,
        (Arr2/tot)*col3 AS c32
FROM  table1 
WHERE col2 = 'x'" 

ありがとう

4

2 に答える 2

1

あなたはこれを試すことができます:

$query = "
SELECT tab.id, tab.Arr1, tab.Arr2, tab.tot, (tab.Arr1/tab.tot) as c31, (tab.Arr2/tab.tot) as c32
from (
SELECT   table1.id,
         COUNT(DISTINCT arr1.element) AS Arr1,
         COUNT(DISTINCT arr2.element) AS Arr2,
         COUNT(DISTINCT arr1.element) +
         COUNT(DISTINCT arr2.element) AS tot
FROM table1 
WHERE col2 = 'x'
) as 'tab' " 
于 2012-12-05T13:13:51.297 に答える
0

これを試して:

SELECT id, Arr1, Arr2, (Arr1 + Arr2) tot, ((Arr1 / (Arr1 + Arr2)) * col3) c31, ((Arr2 / (Arr1 + Arr2)) * col3) c32
FROM (SELECT table1.id, COUNT(DISTINCT arr1.element) AS Arr1, COUNT(DISTINCT arr2.element) AS Arr2, col3
FROM table1 WHERE col2 = 'x') AS A
于 2012-12-05T13:11:12.927 に答える