[英]Complicated SQL query
Does anyone have an idea about how to structure the following query: 有谁知道如何构造以下查询:
Tables : 桌子:
TBL_GAME id, name
TBL_CATEGORY id, name
LU_GAME_TO_CATEGORY gameid, catid
LU_GAME_TO_EVENT eventid, gameid
So, basically Categories
have many games. 因此,基本上Categories
有很多游戏。
Events
have many games. Events
有很多游戏。
I want to generate a report that shows the Categories
listed by how many of its Games
were used in Events
. 我想生成一个报告,以显示在Events
中使用的Games
数量列出的Categories
。 Ordered by the amount descending. 按金额降序排列。
Is this possible ? 这可能吗 ?
SELECT
c.id as catId,
c.name as catName,
g.id as gameId,
g.name as gameName,
sum(ge.INT_QUANTITY) as totalQuantities
FROM
TBL_CATEGORY as c,
TBL_GAME as g,
LU_GAME_TO_CATEGORY as gc,
LU_GAME_TO_EVENT as ge
WHERE
c.id = gc.catId AND
g.id = gc.gameId AND
g.id = ge.gameId
GROUP BY
c.id,
g.id
ORDER BY
totalQuantities desc,
c.name,
g.name
SELECT A.ID, SUM(D.INT_QUANTITY) AS YourSum
FROM TBL_CATEGORY A --Adapt your selected columns
INNER JOIN LU_GAME_TO_CATEGORY B ON A.id = B.catid
INNER JOIN TBL_CATEGORY C ON B.gameid = C.id
INNER JOIN LU_GAME_TO_EVENT D ON C.gameid = D.gameid
GROUP BY A.ID
ORDER BY YourSum DESC;
Here i don't adjust amount, because columns does not exist. 在这里我不调整金额,因为列不存在。 You must add Amount column in target table 您必须在目标表中添加金额列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.