日期:2014-05-18  浏览次数:21008 次

判断某列值是否大于0
判断某列任何一个数大于0返回真T,否则返回假F
SQL2000

表 otherin
列  
  int ,decimal(18,5)
billid ,viplbsl2  
 1 0
 1 2
 1 3
 1 4
 1 0

以上的结果应为真

 

------解决方案--------------------
SQL code

select (case when exists (select 1 from tb where col <= 0) then 'F' else 'T' end)

------解决方案--------------------
SQL code

declare @t table (billid int,viplbsl2 decimal(18,5))
insert into @t
select 1,0 union all
select 1,2 union all
select 1,3 union all
select 1,4 union all
select 1,0

select *,
newcol=case when sign(billid)<>-1 and sign(viplbsl2)<>-1
then 'T' else 'F' END from @t

select *,
newcol=case when billid>0 and viplbsl2>0
then 'T' else 'F' END from @t

/*
billid      viplbsl2                                newcol
----------- --------------------------------------- ------
1           0.00000                                 T
1           2.00000                                 T
1           3.00000                                 T
1           4.00000                                 T
1           0.00000                                 T
*/

------解决方案--------------------
SQL code
--要求:
--这一列如果有其中一个值是大于0的则为T
declare @t table (billid int,viplbsl2 decimal(18,5))
insert into @t
select 1,0 union all
select 1,2 union all
select 1,3 union all
select 1,4 union all
select 1,0
declare @i int
select @i= sum(case when viplbsl2>0 then 1 else 0 end ) from @t
print @i
  
  select (case when @i<= 0  then 'F' else 'T' end)
  
  --只要有一个有一个大于0的就返回T,否则就是F 


 
----
T

(1 行受影响)