基于join和case插入sqlite数据

基于join和case插入sqlite数据,sql,sqlite,join,select,case,Sql,Sqlite,Join,Select,Case,我有两个表:sqlite数据库sqlite版本3.32.2中的每周_分数和团队 每周得分包含团队id和目标差,目标差包含团队在该周赢得的目标数。团队具有不同团队的team.id 我想根据本赛季的最大致胜目标在团队表中对球队进行分类。 如果目标_差值小于2,则团队技能=低 如果目标差>=2,则team.skill=高 我想返回每周_分数表中每个团队的最大目标差异,并根据上述标准对团队表中的技能进行分类 我的代码基于和 任何帮助都会很好 您可以尝试以这种方式插入和选择 insert into tea

我有两个表:sqlite数据库sqlite版本3.32.2中的每周_分数和团队

每周得分包含团队id和目标差,目标差包含团队在该周赢得的目标数。团队具有不同团队的team.id 我想根据本赛季的最大致胜目标在团队表中对球队进行分类。 如果目标_差值小于2,则团队技能=低 如果目标差>=2,则team.skill=高 我想返回每周_分数表中每个团队的最大目标差异,并根据上述标准对团队表中的技能进行分类

我的代码基于和


任何帮助都会很好

您可以尝试以这种方式插入和选择

insert into team(id, skill)
select t.id
, case when t.max_count < 2 then 'low' else 'high' end 
from (
    select team.id, max(weekly_scores.goal_differential) as max_count
    from weekly_scores
    left join team
    on weekly_scores.team_id = team.id
    where weekly_scores.goal_differential is not NULL
    group by team.id
) t 

您需要更新表团队,而不是插入新行。 使用相关子查询:

update team 
set skill = coalesce(
  (
    select case when max(w.goal_differential) < 2 then 'low' else 'high' end 
    from weekly_scores w
    where w.team_id = team.id and w.goal_differential is not NULL 
  ), 
  skill
) 

桌子空了吗?为什么只插入“技能”列?团队表中填写了与团队相关的数据,例如城市、注册年份。“技能”列基于“每周分数表检查我的答案”中的数据。我尝试了此答案,并收到以下错误消息。执行已完成,但有错误。结果:接近;:第9行的语法错误我还尝试在最后一个括号后添加分号,但得到了类似的消息。在子查询中使用分号。。删除答案更新-它工作了。谢谢coalesce执行什么函数?@user2466888 coalesce返回其参数的第一个非空值:
insert into team(id, skill)
select t.id
, case when t.max_count < 2 then 'low' else 'high' end 
from (
    select team.id, max(weekly_scores.goal_differential) as max_count
    from weekly_scores
    left join team
    on weekly_scores.team_id = team.id
    where weekly_scores.goal_differential is not NULL
    group by team.id
) t 
update team 
set skill = coalesce(
  (
    select case when max(w.goal_differential) < 2 then 'low' else 'high' end 
    from weekly_scores w
    where w.team_id = team.id and w.goal_differential is not NULL 
  ), 
  skill
)