您好我对SQL查询的新手我只知道简单的查询。
我的问题是SELECT skipped check number EX2001,EX2002,EX2004
可能
select result will show EX2003
。
提前谢谢,对不起我的英语。
你只能回答算法,我试着把它实现到SQL。
例:
SELECT * FROM SETTLEMENT WHERE checkno not in(在ex2001和ex2900之间)
这样可能吗?即时通讯使用MS SQL 2008。
答案 0 :(得分:0)
如果您在查询中需要硬编码值,那么就可以这样做(因为您没有发布任何代码,所以这里是一个简单的查询)。您可以在sql中使用IN()
或NOT IN()
select * from table where check_number not in ('EX2001','EX2002','EX2004' );
答案 1 :(得分:0)
create table sequence(st varchar(50))
insert into sequence values('EX2001');
insert into sequence values('EX2002');
insert into sequence values('EX2004');
insert into sequence values('EX2005');
insert into sequence values('EX2008');
假设您的原始表格名称为序列且只有一个字段,您可以根据需要进行修改
尝试以下
DECLARE @all TABLE
(
st varchar(20)
)
declare @start int
declare @end int
declare @str varchar(20)
set @start=2000 //define starting point
set @end=2010 //define end point
while(@start<@end)
BEGIN
SET @start=@start+1
set @str='EX'+cast(@start as varchar(20))
INSERT INTO @all VALUES (''+@str+'')
END
SELECT * from @all
except
select * from sequence
输出
st
EX2003
EX2006
EX2007
EX2009
EX2010
答案 2 :(得分:0)
由于未提及SQL引擎,因此该答案仅适用于 Oracle 11G
选项1:使用分层查询
CREATE TABLE TEST1 ( A VARCHAR2 ( 9 ) );
INSERT INTO
TEST1
VALUES
( 'EX2001' );
INSERT INTO
TEST1
VALUES
( 'EX2002' );
INSERT INTO
TEST1
VALUES
( 'EX2004' );
COMMIT;
WITH TEST2
AS (SELECT
TO_NUMBER(SUBSTR ( A,
3 ))
AS A
FROM
TEST1)
SELECT
MIN_A
- 1
+ LEVEL
FROM
(SELECT
MIN ( A ) MIN_A,
MAX ( A ) MAX_A
FROM
TEST2)
CONNECT BY
LEVEL <= MAX_A
- MIN_A
+ 1
MINUS
SELECT A FROM TEST2;
选项2:使用Oracle分析功能
WITH T
AS (SELECT
TO_NUMBER(SUBSTR ( A,
3 ))
AS SNO,
SYSDATE AS SDATE
FROM
TEST1)
SELECT
SDATE,
SNO
+ 1
FIRST_MISSING,
DECODE ( NEXT_SNO
- 1,
SNO
+ 1, TO_NUMBER ( NULL ),
NEXT_SNO
- 1 )
LAST_MISSING
FROM
(SELECT
SDATE,
SNO,
LAG ( SNO )
OVER ( PARTITION BY SDATE
ORDER BY SNO )
LAST_SNO,
LEAD ( SNO )
OVER ( PARTITION BY SDATE
ORDER BY SNO )
NEXT_SNO
FROM
T)
WHERE
NVL ( NEXT_SNO,
SNO
+ 1 ) <> SNO
+ 1;