以其他列

时间:2017-12-15 04:26:52

标签: sql google-bigquery

我的服务器崩溃了,损坏了我的数据库。在我的一个表格中,我有一个academic_quality列,其中我存储了A, B, C+, D等学校成绩。不知何故,我的成绩不是通过某些行上的其他列分发的。我以为我可以创建一个条件子句并创建一个新列academic_quality_new,并在检查其他列后更新它。我尝试了以下查询但它没有用。

SELECT  academic_quality, acceptance_rate, undergrads, setting, environment, degrees_offered,
        CASE WHEN (academic_quality OR acceptance_rate OR undergrads OR setting OR environment OR degrees_offered) = 'A' THEN INSERT 'A' INTO academic_quality_new 
        CASE WHEN (academic_quality OR acceptance_rate OR undergrads OR setting OR environment OR degrees_offered) = 'C' THEN INSERT 'B' INTO academic_quality_new         
FROM [school_List_V4] 

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以尝试为所有可用成绩添加案例..

首先,您可以执行选择查询并检查结果

select * ,
case when academic_quality = 'A' OR acceptance_rate = 'A' then  'A'
else case when academic_quality ='B' OR acceptance_rate = 'B' then  'B'
     else case when  academic_quality = 'C' OR acceptance_rate =  'C' then  'C'
        else '--'
        end
     end
end
from table1

然后尝试此更新查询,它将更新新列。如果2个案例属实,那么它将采用第一种情况,因此成绩的案例应该从最高到最低。

update table1
set new =
case when academic_quality = 'A' OR acceptance_rate = 'A' then  'A'
else case when academic_quality ='B' OR acceptance_rate = 'B' then  'B'
     else case when  academic_quality = 'C' OR acceptance_rate =  'C' then  'C'
        else '--'
        end
     end
end

并且最终更新的新列是从table1

中选择*
academic_quality | acceptance_rate | new
A                | NULL            | A         
NULL             | B               | B         
NULL             | NULL            | --        
A                | B               | A         
C                | B               | B