Here's what I came up with (this isn't tested yet)
CREATE TABLE COUNTRY_MONDIAL
AS
SELECT ROW_NUMBER() OVER (ORDER BY t.Score DESC) AS RowN
, c.Dtf0
, c.Dtf1
, COALESCE((LOG(LOG(c.Dtf0)+1)+1),0)*9.531408863445597
+ COALESCE((LOG(LOG(c.Dtf1)+1)+1), 0)*9.531408863445696 AS Score
, t.Id
, t.ID_CITY
, t.Id AS Id2
, t.dl
FROM ( SELECT i.PKey
, MAX(CASE WHEN i.WordId=205 THEN i.COUNT ELSE 0 END) AS Dtf0
, MAX(CASE WHEN i.WordId=223 THEN i.COUNT ELSE 0 END) AS Dtf1
FROM COUNTRY_I i
WHERE i.WordId IN (205,223)
GROUP
BY i.PKey
HAVING 0 < MAX(CASE WHEN i.WordId=205 THEN i.COUNT ELSE 0 END)
OR 0 < MAX(CASE WHEN i.WordId=223 THEN i.COUNT ELSE 0 END)
) c
JOIN COUNTRY t
ON t.Id = c.PKey
ORDER
BY t.Score DESC;
Some notes:
I don't believe you can reference an alias assigned to an expression in a HAVING clause. (This may have changed, and now be allowed in more recent versions of Oracle, but in the HAVING clause, we had to repeat the expression.
An alias CAN be referenced in an ORDER BY clause (except for UNION/UNION ALL queries); we
We can always reference by column position in the SELECT list.
I'm not sure if we can reference the column alias in the ORDER BY clause, in the ROW_NUMBER() OVER(). The query above does that, but repeating that expression would be the workaround.
Qualifying ALL column references with a table alias is good practice.
We can't have two columns of the same name (Id
) in a table, so at least one of the occurrences will need to be assigned a different name, its better to specify it in our query (we avoid Oracle throwing an error, or from generating an assigned name.)
In the query above, the COALESCE function performs the same duties as NVL()
or CASE expression. (I believe COALESCE is the more portable and SQL-92 compatible option.)
I think COUNT is a keyword, qualifying it with a table alias (i.COUNT
) makes it unambiguous.