SQL:SELECT MAX(Date) 涉及几个表
Posted
技术标签:
【中文标题】SQL:SELECT MAX(Date) 涉及几个表【英文标题】:SQL: SELECT MAX(Date) involving a couple of tables 【发布时间】:2012-02-23 11:39:23 【问题描述】:我有一个包含四个表的小型 MS Access 数据库,并且正在努力处理涉及所有表的 MAX(Date) 查询。任何帮助将不胜感激。
数据库概览:
TABLE COLUMNS
Person Person_ID |....
Games Game_ID | Name |....
Played_Games Played_Games_ID | Game_ID | Date |....
Participation Played_Games_ID | Person_ID |...
列出所有游戏以及我使用的任何人最后一次玩这些游戏的日期:
SELECT DISTINCT Games.Name,
(SELECT MAX(Date) FROM Played_Games WHERE Played_Games.Game_ID = Games.Game_ID) AS Date_Last_Played
FROM Games
ORDER BY Games.Name
这很好用,但我还想列出他们每个人最后一次被某个群体的成员玩的日期。对此的任何帮助将不胜感激。
几天前我问了一个similar question,但后来我的目标是列出所有游戏,每场比赛总共玩了多少次,以及某个群体的成员每场比赛玩了多少次(人 1 和 2 只是一个例子)。 GarethD 通过下面的代码帮助我解决了这个问题。我试图改变这段代码来解决我的新任务,因为我猜这个逻辑/结构的大部分可以重复使用,但到目前为止我自己还没有成功:-(
SELECT Games.Name,
IIF(ISNULL(TimesPlayed),0,TimesPlayed) AS Times_Played,
IIF(ISNULL(TimesPlayedByGroupMembers),0,TimesPlayedByGroupMembers) AS Times_Played_By_Group_Members
FROM Games
LEFT JOIN
( SELECT Game_ID,
COUNT(*) AS TimesPlayed,
SUM(IIF(ISNULL(Participation.Played_Games_ID),0,1)) AS TimesPlayedByGroupMembers
FROM Played_Games
LEFT JOIN
( SELECT Played_games_ID
FROM Participation
WHERE Person_ID IN (1, 2)
GROUP BY Played_games_ID
) AS Participation
ON Participation.Played_Games_ID = Played_Games.Played_Games_ID
GROUP BY Game_ID
) AS Played_Games
ON Played_Games.Game_ID = Games.Game_ID
ORDER BY Games.Name
任何帮助将不胜感激。感谢您的宝贵时间!
【问题讨论】:
“某组人的成员”不确定这是什么意思。您是否只需要添加一个 where 子句以便获得您想要的玩家?就像 Person_ID in (...) 一样? 感谢您的快速回复,并对我迟到的回复感到抱歉。...AND Person_ID IN (...)
在这种情况下是不够的,因为您必须找出、隔离然后仅对与这些特定人员相关的 Played_Games_ID 进行操作。感谢您的回复和您的帮助意愿,但问题现在由GarethD 解决。谢谢
【参考方案1】:
我刚刚在我发布的最后一个答案中添加了几行,以显示最后一次播放的日期,以及组成员最后一次播放的日期,以及播放次数旁边。希望这就是你所追求的。
SELECT Games.Name,
IIF(ISNULL(TimesPlayed),0,TimesPlayed) AS Times_Played,
IIF(ISNULL(TimesPlayedByGroupMembers),0,TimesPlayedByGroupMembers) AS Times_Played_By_Group_Members,
LastPlayed,
LastPlayedByGroupMembers
FROM Games
LEFT JOIN
( SELECT Game_ID,
COUNT(*) AS TimesPlayed,
SUM(IIF(ISNULL(Participation.Played_Games_ID),0,1)) AS TimesPlayedByGroupMembers,
MAX(Played_Games.[Date]) AS LastPlayed,
MAX(IIF(ISNULL(Participation.Played_Games_ID),NULL,Played_Games.[Date])) AS LastPlayedByGroupMembers
FROM Played_Games
LEFT JOIN
( SELECT Played_games_ID
FROM Participation
WHERE Person_ID IN (1, 2)
GROUP BY Played_games_ID
) AS Participation
ON Participation.Played_Games_ID = Played_Games.Played_Games_ID
GROUP BY Game_ID
) AS Played_Games
ON Played_Games.Game_ID = Games.Game_ID
ORDER BY Games.Name
【讨论】:
感谢您的快速回复,并对我迟到的回复感到抱歉。您的新增功能完美地解决了我的问题。再次感谢您的帮助 GarethD 你能告诉我你为什么顺便把日期放在括号里吗?该查询似乎在没有它们的情况下与 og 一起工作,所以我只是好奇括号是什么(通常) 它们用于消除 SQL 关键字和对象名称之间的歧义,或者当对象名称中有空格时(通常可以通过良好的设计避免)。例如如果您有一个包含名为“Select”的列的表并编写了以下查询SELECT select FROM Table
,您会得到一个不正确的语法错误,但是使用SELECT [select] FROM Table
查询然后知道[select] 指的是“select”列而不是比 SQL 关键字 SELECT
。 - ***.com/questions/153861/brackets-in-sql-statements以上是关于SQL:SELECT MAX(Date) 涉及几个表的主要内容,如果未能解决你的问题,请参考以下文章
SQL 在 Select 中使用 SUM() 在视图上创建索引
选择具有最大值的行,并结合WHERE。 MAX和CAST,在spark.sql中
SQL 查询:HAVING date = MAX(date) 不起作用