如何从varchar中提取整数而不创建错误
我需要构建查询,并提供与学生数量和学生类型的课程列表。但是,在这所特定的学校中,一个课程可以包含一个或多个学校的级别。例如,有些学生倾向于在某些学校级别之间的同一班级中教授。例如某些残障学生。可以与其他学生一起教授特殊学生。 我需要那个班级的主要学校级别。检索该级别的唯一方法是基于类名。 因此,我有下表的课程
编号 | 学生 | 学校级别 | 编号专业 | 学生 |
---|---|---|---|---|
编号1A | 25 | 0,1 | 5 | 0 |
1B | 22 | 0,1 | 7 | 0 |
2A | 26 | 2 | 5 | 0 |
2B | 23 | 2 | 5 0 | 3A 2 5 0 |
3A | 28 | 3 | 4 | 0 |
3B 27 3B | 27 | 3 | 3 | 0 |
NVSK | 12 | 0,1,2 | 0 | 12 |
此顶部列表是我拥有的样本。 列表
班级 | 人员 | 学生 | 教学 | 编号 | 。 |
---|---|---|---|---|---|
以下 | , | , | 现在 | 来 | 我 |
方式 | 编号 | 按照 | 需要 | 制作 | |
| | | | | |
| | | | 5 | 0 |
3a | 3 | 28 | 3 | 4 | 0 |
3b | 3 | 27 | 3 | 3 | 0 |
nvsk | null | 12 | 0,1,2 | 0 | 12 |
我如何创建一个称为主要学校级别(type int)的字段,该字段可以从className中提取第一个值然后将其变成数字字段?
您可能已经注意到,列表中还有一个名为“ NVSK”的类,无法将其翻译成一个数字,那么我如何能够放置零值?在这种情况下最好使用哪种技术?
这是查询的形式
select c.shortname as Classname, //varchar type
// as "MainSchoolLevel"
count(s.studentid) as "No.ofStudents",
string_agg(distinct cast(l.shorttext as varchar(4)), ', ') as "SchoolLevels",
sum(case when s.specialstudent true then 1 else 0 end) as "No.ofSpecialStudents",
sum(case when s.handicappedstudent true then 1 else 0 end) as "No.ofhandicappedStudents",
from student s
join studentclass sc on sc.studentid = sc.id
join class c on c.id = sc.classid
join classlevel cl on cl.classid = c.id
join level on l.id = cl.levelid
I am required to build a query and provide a list of classes with number of students and type of students. However in this particular school, one class can contain one or more school levels. For example some students tend to be taught in the same class between certain school levels. e.g. certain handicapped students. Special students can be taught with rest of the students.
I need a dominant school level from that class. The only way to retrieve that level is based on the class name.
So I have the following table
Class | No.ofStudents | School Levels | No.ofSpecialStudents | No.ofhandicappedstudents |
---|---|---|---|---|
1A | 25 | 0,1 | 5 | 0 |
1B | 22 | 0,1 | 7 | 0 |
2A | 26 | 2 | 5 | 0 |
2B | 23 | 2 | 5 | 0 |
3A | 28 | 3 | 4 | 0 |
3B | 27 | 3 | 3 | 0 |
NVSK | 12 | 0,1,2 | 0 | 12 |
This top list is a sample of what I have. Now I need to the produce the list as follows
Class | MainSchoollevel | No.ofStudents | SchoolLevels | No.ofSp.Students | No.ofHCstudents |
---|---|---|---|---|---|
1A | 1 | 25 | 0,1 | 5 | 0 |
1B | 1 | 22 | 0,1 | 7 | 0 |
2A | 2 | 26 | 2 | 5 | 0 |
2B | 2 | 23 | 2 | 5 | 0 |
3A | 3 | 28 | 3 | 4 | 0 |
3B | 3 | 27 | 3 | 3 | 0 |
NVSK | null | 12 | 0,1,2 | 0 | 12 |
How would I be able to create a field called Main school Level (type int) that could extract the first value from the classname and then turn it into a number field?
As you might have noticed there is also a class named 'NVSK' in the list, that cannot be translated into a number, so how would I be able to place a null value instead? What technique would best be used for this situation?
Here is the form of the query
select c.shortname as Classname, //varchar type
// as "MainSchoolLevel"
count(s.studentid) as "No.ofStudents",
string_agg(distinct cast(l.shorttext as varchar(4)), ', ') as "SchoolLevels",
sum(case when s.specialstudent true then 1 else 0 end) as "No.ofSpecialStudents",
sum(case when s.handicappedstudent true then 1 else 0 end) as "No.ofhandicappedStudents",
from student s
join studentclass sc on sc.studentid = sc.id
join class c on c.id = sc.classid
join classlevel cl on cl.classid = c.id
join level on l.id = cl.levelid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试通过正则表达式提取值并将其分配给新列
Try to extract values by regular expression and assign them to new columns