如何从varchar中提取整数而不创建错误

发布于 2025-01-24 04:37:49 字数 2380 浏览 2 评论 0原文

我需要构建查询,并提供与学生数量和学生类型的课程列表。但是,在这所特定的学校中,一个课程可以包含一个或多个学校的级别。例如,有些学生倾向于在某些学校级别之间的同一班级中教授。例如某些残障学生。可以与其他学生一起教授特殊学生。 我需要那个班级的主要学校级别。检索该级别的唯一方法是基于类名。 因此,我有下表的课程

编号学生学校级别编号专业学生
编号1A250,150
1B220,170
2A26250
2B2325 03A 2 5 0
3A28340
3B 27 3B27330
NVSK120,1,2012

此顶部列表是我拥有的样本。 列表

班级人员学生教学编号
以下现在
方式编号按照需要制作
50
3a328340
3b327330
nvsknull120,1,2012

我如何创建一个称为主要学校级别(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

ClassNo.ofStudentsSchool LevelsNo.ofSpecialStudentsNo.ofhandicappedstudents
1A250,150
1B220,170
2A26250
2B23250
3A28340
3B27330
NVSK120,1,2012

This top list is a sample of what I have. Now I need to the produce the list as follows

ClassMainSchoollevelNo.ofStudentsSchoolLevelsNo.ofSp.StudentsNo.ofHCstudents
1A1250,150
1B1220,170
2A226250
2B223250
3A328340
3B327330
NVSKnull120,1,2012

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2025-01-31 04:37:49

尝试通过正则表达式提取值并将其分配给新列

select
  class,
  (coalesce (substring (class, '^\d+'), '') as num_class,
  (coalesce (substring (class, '[A-Za-z]+'), '') as str_class,
from student s ....

Try to extract values by regular expression and assign them to new columns

select
  class,
  (coalesce (substring (class, '^\d+'), '') as num_class,
  (coalesce (substring (class, '[A-Za-z]+'), '') as str_class,
from student s ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文