我想从足球数据库中提取一些统计数据。特别是,我想提取每个团队的最后5场比赛。这可能与SQLite3有关吗?
我已经能够使用以下查询对所有记录执行此操作(不太花哨,但它可以工作!):</ p>
SELECT home_stats.team_name,
home_matches,
home_won,
home_drawn,
home_lost,
away_matches,
away_won,
away_drawn,
away_lost
FROM
(SELECT soccer."date" AS "date", home_team AS team_name,
COUNT(*) as home_matches,
COUNT(CASE WHEN(SUBSTR(FTR,0,INSTR(FTR,":")) > SUBSTR(FTR,INSTR(FTR,":") + 1, LENGTH(FTR) - INSTR(FTR, ":") + 1)) THEN 1 ELSE NULL END) AS home_won,
COUNT(CASE WHEN(SUBSTR(FTR,0,INSTR(FTR,":")) = SUBSTR(FTR,INSTR(FTR,":") + 1, LENGTH(FTR) - INSTR(FTR, ":") + 1)) THEN 1 ELSE NULL END) AS home_drawn,
COUNT(CASE WHEN(SUBSTR(FTR,0,INSTR(FTR,":")) < SUBSTR(FTR,INSTR(FTR,":") + 1, LENGTH(FTR) - INSTR(FTR, ":") + 1)) THEN 1 ELSE NULL END) AS home_lost
FROM soccer
WHERE(soccer.FTR NOT NULL)
GROUP BY soccer.home_team) AS home_stats
JOIN
(SELECT soccer."date" AS "date", soccer.away_team AS team_name,
COUNT(*) as away_matches,
COUNT(CASE WHEN(SUBSTR(soccer.FTR,0,INSTR(soccer.FTR,":")) < SUBSTR(soccer.FTR,INSTR(soccer.FTR,":") + 1, LENGTH(soccer.FTR) - INSTR(soccer.FTR,":") + 1)) THEN 1 ELSE NULL END) AS away_won,
COUNT(CASE WHEN(SUBSTR(soccer.FTR,0,INSTR(soccer.FTR,":")) = SUBSTR(soccer.FTR,INSTR(soccer.FTR,":") + 1, LENGTH(soccer.FTR) - INSTR(soccer.FTR,":") + 1)) THEN 1 ELSE NULL END) AS away_drawn,
COUNT(CASE WHEN(SUBSTR(soccer.FTR,0,INSTR(soccer.FTR,":")) > SUBSTR(soccer.FTR,INSTR(soccer.FTR,":") + 1, LENGTH(soccer.FTR) - INSTR(soccer.FTR,":") + 1)) THEN 1 ELSE NULL END) AS away_lost
FROM soccer
WHERE(soccer.FTR NOT NULL)
GROUP BY soccer.away_team) AS away_stats
ON (home_stats.team_name = away_stats.team_name);