使用IF-THE或CASE-然后从数据集中进行选择
我有一个一年级、二年级和三年级学生的数据集。我想选择一年级和二年级所有学生的姓名,但只选择三年级名为 John、Jane 或 Smith 的学生。到目前为止,这是我所得到的:
select
first_name,
grade_level
from table_with_all_students_info
-- I need an if or case when statement here saying if student is in third grade, their first name selected can only be John, Jane or Smith. My attempt is below.
case when grade_level in ('3rd grade')
then first_name in ('John', 'Jane', 'Smith')
我不确定我在那里出了什么问题,但我希望得到一些帮助。谢谢
I have a dataset with students in 1st, 2nd and 3rd grades. I want to select the names of all students in 1st and 2nd grades, but only students named John, Jane or Smith from 3rd grade. Here is what I have so far:
select
first_name,
grade_level
from table_with_all_students_info
-- I need an if or case when statement here saying if student is in third grade, their first name selected can only be John, Jane or Smith. My attempt is below.
case when grade_level in ('3rd grade')
then first_name in ('John', 'Jane', 'Smith')
I'm not sure what I'm getting wrong there but I'd appreciate some help. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以对两个查询进行 UNION,如下所示:
You can UNION two queries such as this:
将
where
与or
结合使用:如果您的数据中只有 3 个成绩,则可以将
grade_level 替换为 ('1stgrade', '2ndgrade') 与
grade_level != '三年级'
Use
where
withor
:If there are only 3 grades in your data, you can replace
grade_level in ('1st grade', '2nd grade')
withgrade_level != '3rd grade'