我正在尝试在Oracle中运行此查询
SELECT A1.ID_USER,A1.CNAME,
(SELECT CNAME
FROM
(SELECT CNAME,COUNTER
FROM
(SELECT id_Category AS IDCATEGORT,COUNT(*) AS COUNTER
FROM gamescateg
WHERE id_game IN
(SELECT ID_GAME
FROM UserGames
WHERE id_user=UsersGamers.ID_USER
)
GROUP BY id_Category
)
JOIN Categories
ON id_Category =IDCATEGORT
ORDER BY COUNTER DESC
)
WHERE rownum = 1
) AS FAVORITECATEGORY
FROM UsersGamers A1
JOIN UsersGamers A2 ON A1.ID_USER= A2.ID_USER
但是我得到了错误
ORA-00904: "A2"."ID_USER": invalid identifier
A2.ID_USER需要做子查询 为什么? 怎么解决? 你可以帮帮我吗?
我通过创造功能来解决这个问题
CREATE OR REPLACE FUNCTION FAVORITECATEGORYfunc( idnumber NUMBER )
RETURN VARCHAR2
IS
l_conc_names VARCHAR2(32676);
BEGIN
SELECT CNAME INTO l_conc_names
FROM
(SELECT CNAME,COUNTER
FROM
(SELECT id_Category AS IDCATEGORT,COUNT(*) AS COUNTER
FROM gamescateg
WHERE id_game IN
(SELECT ID_GAME
FROM UserGames
WHERE id_user=idnumber
)
GROUP BY id_Category
)
JOIN Categories
ON id_Category =IDCATEGORT
ORDER BY COUNTER DESC
)
WHERE rownum = 1;
RETURN l_conc_names;
END;
,查询是
select CNAME,FAVORITECATEGORYfunc(id_user) from usersgamers
感谢您的帮助:)
答案 0 :(得分:1)
基本上,表面修复不会修复查询。所以,请解释你想要做什么。我怀疑查询比它需要的要复杂得多。而且,很难破译。
从表面上看,您的问题是使用表名而不是别名。因此,人们会认为像这样编写查询可以解决问题:
SELECT A1.ID_USER, A1.CNAME,
(SELECT CNAME
FROM (SELECT CNAME, COUNTER
FROM (SELECT id_Category AS IDCATEGORT, COUNT(*) AS COUNTER
FROM gamescateg
WHERE id_game IN (SELECT ug.ID_GAME
FROM UserGames ug
WHERE ug.id_user = UsersGamers.ID_USER
)
GROUP BY id_Category
) cc JOIN
Categories c
ON c.id_Category = cc.IDCATEGORT
ORDER BY COUNTER DESC
)
WHERE rownum = 1
) AS FAVORITECATEGORY
FROM UsersGamers A1 JOIN
UsersGamers A2
ON A1.ID_USER = A2.ID_USER;
但是,唉,它不会。 Oracle限制了相关子查询的范围深度。所以,这并没有解决问题;你只会得到另一个错误。
此类查询可能正在做的最合理的事情是获取给定用户的最常见类别。如果是这样的话:
SELECT u.*
FROM (SELECT ugr.ID_USER, ugr.CNAME, c.cname, COUNT(*) as cnt,
ROW_NUMBER() OVER (PARTITION BY ugr.ID_USER, ugr.CNAME
ORDER BY COUNT(*) DESC
) as seqnum
FROM UsersGamers ugr JOIN
UserGames ug
ON ugr.ID_USER = ug.ID_USER JOIN
Categories c
ON c.id_Category = ug.id_Category
GROUP BY ugr.ID_USER, ugr.CNAME
) u
WHERE seqnum = 1;
答案 1 :(得分:0)
我已经扩展了您的查询以使其更具可读性并对问题进行了评论(无效的标识符)。这里有对别名A2的引用,其中A2不可能。
select A1.ID_USER, A1.CNAME,
(
SELECT CNAME FROM
(
SELECT CNAME, COUNTER
FROM
(
SELECT id_Category AS IDCATEGORT,
COUNT(*) AS COUNTER
FROM gamescateg
WHERE id_game IN
(
SELECT ID_GAME FROM UserGames
WHERE id_user=A2.ID_USER /* your problem is here */
)
GROUP BY id_Category
)
JOIN Categories
ON id_Category =IDCATEGORT
ORDER BY COUNTER DESC
)
WHERE rownum = 1
) AS FAVORITECATEGORY
FROM UsersGamers A1
JOIN UsersGamers A2
ON A1.ID_USER= A2.ID_USER
您是否考虑过重写以避免这么多嵌套查询?不是将它们包装在另一个中,而是尝试使用连接围绕喜欢的类别构建它。
答案 2 :(得分:0)
尝试这样写,并确保获得理想的结果。
A 4 x 6 matrix that wraps around in steps of `3i` from `i` = 4
12 15 18 21
24 27 30 33
36 39 42 45
48 51 54 57
60 63 66 69
72 75 78 81
A 6 x 5 matrix with rows of `3i` for initial `i` in <11,13,17,19,23>
33 36 39 42 45 48
39 42 45 48 51 54
51 54 57 60 63 66
57 60 63 66 69 72
69 72 75 78 81 84
A 5 x 1 matrix with rows of of ` 3i` for initial `i` in <7>
21 24 27 30 33