我想要做的是使用case-when将多行转换为单行,但我得到
ORA-00979:不是GROUP BY表达式
这是我的查询
SELECT P.APPDISPLAYNAME AS PLAYERNAME ,
CASE WHEN GS.SETNUMBER = 1 THEN SS.SETSCORE END AS set1,
CASE WHEN GS.SETNUMBER = 2 THEN SS.SETSCORE END AS set2,
CASE WHEN GS.SETNUMBER = 3 THEN SS.SETSCORE END AS set3,
CASE WHEN GS.SETNUMBER = 4 THEN SS.SETSCORE END AS set4
FROM TENNISMATCH TM ,
TEAM T ,
TEAMPLAYER TP ,
TOURNAMENTPLAYER TRP ,
PLAYER P ,
SETSCORE SS ,
GAMESET GS ,
PLAYERBIO PB
WHERE TM.EVENTTYPE = 'MEN_SINGLES'
AND TM.MATCHSTATUS = 'LIVE'
AND TM.TENNISMATCHID = T.TENNISMATCHID
AND T.TEAMID = TP.TEAMID
AND TP.TOURNAMENTPLAYERID = TRP.TOURNAMENTPLAYERID
AND TRP.PLAYERID = P.PLAYERID
AND T.TEAMID = SS.TEAMID
AND SS.GAMESETID = GS.GAMESETID
AND P.PLAYERID = PB.PLAYERID
GROUP BY P.APPDISPLAYNAME
答案 0 :(得分:0)
SELECT
子句中显示的所有列都必须包含在您的GROUP BY
子句中,或者应用某种Aggregate function。
在您的情况下,如果您只想返回一行,则应该使用MAX()
函数应用于每个CASE
结果。
如果CASE
语句可以为每一行返回不同的结果,那么这当然不准确。但如果是这样的话,那么你的问题就无法解决了。
尝试将其添加到每个CASE
:
MAX(CASE WHEN GS.SETNUMBER=1 THEN SS.SETSCORE END) as set1
答案 1 :(得分:0)
您必须将SELECT
的所有列放在GROUP BY
中,或使用汇总函数将结果压缩为单个值(例如MIN
,MAX
或SUM
)。
答案 2 :(得分:0)
首先,您应该学会使用正确的显式JOIN
语法。甚至Oracle现在也建议使用它。
其次,您的SELECT
需要聚合功能。我会推荐这样的东西:
SELECT P.APPDISPLAYNAME AS PLAYERNAME ,
MAX(CASE WHEN GS.SETNUMBER = 1 THEN SS.SETSCORE END) AS set1,
MAX(CASE WHEN GS.SETNUMBER = 2 THEN SS.SETSCORE END) AS set2,
MAX(CASE WHEN GS.SETNUMBER = 3 THEN SS.SETSCORE END) AS set3,
MAX(CASE WHEN GS.SETNUMBER = 4 THEN SS.SETSCORE END) AS set4
. . .