select 姓名 , 语文 , (case when 数学>=80 then 数学 else 0 end) as 数学 from ta
--建立测试环境 set nocount on create table test(姓名 varchar(20),语文 varchar(20),数学 varchar(20)) insert into test select '小明','70','80' insert into test select '小李','95','90' go --测试 select 姓名, case when 数学=80 then 语文 else 0 end, case when 数学=80 then 数学 else 0 end from test --删除测试环境 drop table test set nocount off
select 姓名,case when 数学 >80 then 语文 else 0 end 语文, case when 数学 >80 then 数学 else 0 end 数学 from ta
select 姓名, (case when 数学>=80 then 语文 else 0 end) as 语文, (case when 数学>=80 then 数学 else 0 end) as 数学 from ta
select 姓名,case when 数学 =80 then 语文 else 0 end 语文, case when 数学 =80 then 数学 else 0 end 数学 from ta
set nocount on
create table test(姓名 varchar(20),语文 varchar(20),数学 varchar(20))
insert into test select '小明','70','80'
insert into test select '小李','95','90'
go
--测试
select 姓名,
case when 数学=80 then 语文 else 0 end,
case when 数学=80 then 数学 else 0 end
from test --删除测试环境
drop table test
set nocount off
case when 数学 >80 then 数学 else 0 end 数学
from ta
(case when 数学>=80 then 数学 else 0 end) as 数学 from ta
case when 数学 =80 then 数学 else 0 end 数学
from ta
/******************************************************************************/
/*回复:20080520005总:00034 */
/*主题:条件查询 */
/*作者:二等草 */
/******************************************************************************/set nocount on--数据--------------------------------------------------------------------------
declare @a table([姓名] varchar(4),[语文] int,[数学] int)
insert into @a select '小明',70,80
insert into @a select '小李',95,90--代码--------------------------------------------------------------------------
select 姓名,
语文=case when 数学=80 then 语文 else 0 end,
数学=case when 数学=80 then 数学 else 0 end
from @a
go/*结果--------------------------------------------------------------------------
姓名 语文 数学
---- ----------- -----------
小明 70 80
小李 0 0
--清除------------------------------------------------------------------------*/
select 语文 = case when 数学 = 80 then 语文 else 0 end,
数学 = case when 数学 = 80 then 80 else 0 end
from A
select 姓名,语文=case when 数学=80 then 语文 else 0 end,
数学=case when 数学=80 then 数学 else 0 end
from 表
Insert Into @1
Select '小明', 70, 80 Union All
Select '小李', 95, 90 Select
[姓名],
[语文]=(1^Abs(Sign([数学]-80)))*[语文],
[语文]=(1^Abs(Sign([数学]-80)))*[数学]
From @1/*
姓名 语文 数学
小明 70 80
小李 95 90
*/
Insert Into @1
Select '小明', 70, 80 Union All
Select '小李', 95, 90 Select
[姓名],
[语文]=(1^Abs(Sign([数学]-80)))*[语文],
[数学]=(1^Abs(Sign([数学]-80)))*[数学]
From @1/*
姓名 语文 数学
小明 70 80
小李 0 0
*/