如何在 SQL 服务器中获取字符串形式的值
How to get value as string in SQL Server
我有一个 table tbl_Marks
:
Std_id | sub_id | term_I | term_2
--------+---------+-----------+-------
std_1 | 1 | 40 | 50
std_1 | 2 | 30 | 40
std_1 | 3 | 20 | 30
std_1 | 4 | 10 | 50
std_2 | 1 | 50 | 50
std_2 | 2 | 50 | 50
std_2 | 3 | 50 | 50
std_2 | 4 | 50 | 50
我怎样才能得到这样的结果:
Std_id | sub_id | term_I | term_2 | total | status | PROMOTION_status
--------+---------+-----------+--------+--------+--------+------------------
std_1 | 1 | 40 | 50 | 90 | PASS | REPEATER
std_1 | 2 | 30 | 40 | 70 | PASS | REPEATER
std_1 | 3 | 20 | 20 | 40 | FAIL | REPEATER
std_1 | 4 | 10 | 50 | 60 | PASS | REPEATER
注意:如果总值小于任何 sub_id
的 50
std_2 | 1 | 50 | 50 | 100 | PASS | PROMOTED
std_2 | 2 | 50 | 50 | 100 | PASS | PROMOTED
std_2 | 3 | 50 | 50 | 100 | PASS | PROMOTED
std_2 | 4 | 50 | 50 | 100 | PASS | PROMOTED
注意:如果总值大于 50 或等于每个 sub_id
请帮忙!
使用CASE
。
select t.*,
t.term_I + t.term_2 as total,
case when t.term_I + t.term_2 >= 50 then 'pass' else 'fail' end as status,
case when t.std_id = 'std_2' then 'PRMOTED' else 'REAPEATER' end as promotion_status
from tbl_marks t
以下 SQL Select 语句可以提供帮助
除了 SQL CTE expression 你还可以使用 subselect 语句
但是这个查询的主要技巧是使用 MIN aggregation function with Partition by 子句
这可以帮助您检查该学生的分数是否低于 50,如果是,它会将该学生的所有记录设为重复状态
;with cte as (
select
*,
term_1 + term_2 as total
from tbl_Marks
)
select
*,
case when (term_1 + term_2) >= 50 then 'PASS' else 'FAIL' end as status,
case when (
min(total) over (partition by Std_id)
) >= 50 then 'PROMOTED' else 'REPEATER' end as PROMOTION_status
from cte
希望对您有所帮助,
我有一个 table tbl_Marks
:
Std_id | sub_id | term_I | term_2
--------+---------+-----------+-------
std_1 | 1 | 40 | 50
std_1 | 2 | 30 | 40
std_1 | 3 | 20 | 30
std_1 | 4 | 10 | 50
std_2 | 1 | 50 | 50
std_2 | 2 | 50 | 50
std_2 | 3 | 50 | 50
std_2 | 4 | 50 | 50
我怎样才能得到这样的结果:
Std_id | sub_id | term_I | term_2 | total | status | PROMOTION_status
--------+---------+-----------+--------+--------+--------+------------------
std_1 | 1 | 40 | 50 | 90 | PASS | REPEATER
std_1 | 2 | 30 | 40 | 70 | PASS | REPEATER
std_1 | 3 | 20 | 20 | 40 | FAIL | REPEATER
std_1 | 4 | 10 | 50 | 60 | PASS | REPEATER
注意:如果总值小于任何 sub_id
的 50std_2 | 1 | 50 | 50 | 100 | PASS | PROMOTED
std_2 | 2 | 50 | 50 | 100 | PASS | PROMOTED
std_2 | 3 | 50 | 50 | 100 | PASS | PROMOTED
std_2 | 4 | 50 | 50 | 100 | PASS | PROMOTED
注意:如果总值大于 50 或等于每个 sub_id
请帮忙!
使用CASE
。
select t.*,
t.term_I + t.term_2 as total,
case when t.term_I + t.term_2 >= 50 then 'pass' else 'fail' end as status,
case when t.std_id = 'std_2' then 'PRMOTED' else 'REAPEATER' end as promotion_status
from tbl_marks t
以下 SQL Select 语句可以提供帮助 除了 SQL CTE expression 你还可以使用 subselect 语句 但是这个查询的主要技巧是使用 MIN aggregation function with Partition by 子句 这可以帮助您检查该学生的分数是否低于 50,如果是,它会将该学生的所有记录设为重复状态
;with cte as (
select
*,
term_1 + term_2 as total
from tbl_Marks
)
select
*,
case when (term_1 + term_2) >= 50 then 'PASS' else 'FAIL' end as status,
case when (
min(total) over (partition by Std_id)
) >= 50 then 'PROMOTED' else 'REPEATER' end as PROMOTION_status
from cte
希望对您有所帮助,