大家好我求一条SQL语句,数据库中表TB1内容如下:姓名 计划件数 欠缺件数
甲 20 -10
乙 0 20
丙 30 -5
丁 0 -6
甲甲 10 5
甲乙 0 -2
…………………………………………
……………………
要求如下:查询 计划件数>0 或者 欠缺件数>0 的结果,也就是说查询 计划件数和欠缺件数不同时<=0 以外的数据
请问该怎么写这条语句啊
甲 20 -10
乙 0 20
丙 30 -5
丁 0 -6
甲甲 10 5
甲乙 0 -2
…………………………………………
……………………
要求如下:查询 计划件数>0 或者 欠缺件数>0 的结果,也就是说查询 计划件数和欠缺件数不同时<=0 以外的数据
请问该怎么写这条语句啊
INSERT INTO #TB select '甲', 20, -10
union all select '乙', 0, 20
union all select '丙', 30, -5
union all select '丁', 0, -6
union all select '甲甲', 10, 5
union all select '甲乙', 0, -2
SELECT * FROM #tb
WHERE (计划件数>0 OR 欠缺件数>0 )
/*
姓名 计划件数 欠缺件数
---------- ----------- -----------
甲 20 -10
乙 0 20
丙 30 -5
甲甲 10 5
(4 行受影响)
*/
----------------------------------------------------------------
-- Author :TravyLee(物是人非事事休,欲语泪先流!)
-- Date :2012-10-16 20:00:37
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 <X86> (Build 7600: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
go
create table [test](
[姓名] varchar(4),
[计划件数] int,
[欠缺件数] int
)
insert [test]
select '甲',20,-10 union all
select '乙',0,20 union all
select '丙',30,-5 union all
select '丁',0,-6 union all
select '甲甲',10,5 union all
select '甲乙',0,-2
go
select
*
from
test
where
([计划件数]>0 and [欠缺件数]<=0)
or ([计划件数]<=0 and [欠缺件数]>0)
or ([计划件数]>0 and [欠缺件数]>0)
----------------结果----------------------------
/*
姓名 计划件数 欠缺件数
---- ----------- -----------
甲 20 -10
乙 0 20
丙 30 -5
甲甲 10 5(4 行受影响)
*/
是这个意思么
from tb
where not (计划件数<0 and 欠缺件数<0)
SELECT * FROM TB WHERE (计划件数>0 OR 欠缺件数>0) AND 月份='10'
INSERT INTO #TB select '甲', 20, -10
union all select '乙', 0, 20
union all select '丙', 30, -5
union all select '丁', 0, -6
union all select '甲甲', 10, 5
union all select '甲乙', 0, -2
SELECT * FROM #tb
WHERE (计划件数>0 OR 欠缺件数>0 ) AND DATEPART(MONTH,月份)=10