如何在SQL结果中排除某个范围? [英] How to exclude a certain range in SQL results?

查看:1880
本文介绍了如何在SQL结果中排除某个范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想复制q SQL表中的某个范围并输入到新表中。我还需要排除部分结果。



我在一些论坛和一些SQL教程网站上看到他们大多使用BETWEEN STATEMENT但这只能用于单个条件。



我的条件是复制某个范围,包括2个条件,例如具有特定部件号的开始复制,并以类型结束即A但此数据必须在之后,因此它可以是一个范围。



更新 - 下面链接是完整的问题。



如何在SQL结果中排除某个范围? - 堆栈溢出 [ ^ ]



全部谢谢。





I would like to copy a certain range within q SQL table and input into new table. I also need to exclude part of the results.

I have seen on some forums and some SQL tutorial websites that they mostly use BETWEEN STATEMENT but this only can use in single condition.

My condition is copy the certain range with 2 conditions such as start copy with specific part number and end with type that is "A" but this data must be after ,so it can be a range.

UPDATED-- Below Link is the full question.

How to exclude a certain range in SQL results? - Stack Overflow[^]

Thanks All.


NO	TYPE	Part	Quantity	other
1	Z       1001	1	
2	A       1002	1	
3	T       1003	1	
4	C       1004	1	
5	F       1005	1	
6	V       1006	1	
7	C       1007	1	
8	B       1008	1	
9	D       1009	1	
10	S       1010	1	
11	V       1011	1	
12	Z       1012	1	
13	X       1013	1	
14	A       1014	1	
15	H       1015	1	
16	V       1016	1	
17	L       1017	1	
18	A       1018	1	



显示一些样本数据的另一种方式


Another way of showing some sample data

create table demo (NO int identity(1,1)
,	TYPE	char(1)
,   Part	int
,	Quantity int
, other varchar(10) NULL)
insert into demo (TYPE, Part, Quantity) VALUES
('Z',	1001,	1	),
('A',	1002,	1	),
('T',	1003,	1	),
('C',	1004,	1	),
('F',	1005,	1	),
('V',	1006,	1	),
('C',	1007,	1	),
('B',	1008,	1	),
('D',	1009,	1	),
('S',	1010,	1	),
('V',	1011,	1	),
('Z',	1012,	1	),
('X',	1013,	1	),
('A',	1014,	1	),
('H',	1015,	1	),
('V',	1016,	1	),
('L',	1017,	1	),
('A',	1018,	1	)



预期结果


Expected results

NO	TYPE	Part	Quantity	other
2	A       1002	1	
3	T       1003	1	
4	C       1004	1	
5	F       1005	1	
6	V       1006	1	
7	C       1007	1	
8	B       1008	1	
9	D       1009	1	
10	S       1010	1	
11	V       1011	1	
12	Z       1012	1	
13	X       1013	1	
14	A       1014	1





我的尝试:



我在一些论坛和一些SQL教程网站上看到过



What I have tried:

I have seen on some forums and some SQL tutorial websites

推荐答案

首先找出你要停止的点,例如
First work out the point at which you want to stop e.g.
DECLARE @endNO INT = (SELECT MIN(Part) FROM demo WHERE Part > 1002 AND [TYPE]='A')
PRINT @endNO

在这种情况下返回 1014

然后将其插入BETWEEN的范围,例如

Which in this case returns 1014
Then plug that into your range for BETWEEN e.g.

SELECT   NO, TYPE, Part, Quantity, other 
FROM demo
WHERE Part BETWEEN 1002 AND @endNO

虽然这看起来有点做作,看起来好像你依赖于零件编号的顺序。我不是说你做的是错的,但看起来确实有些奇怪。

It all seems a bit contrived though and looks as if you are depending on the order of the part numbers. I'm not saying what you are doing is wrong, but it does seem a little strange.


这篇关于如何在SQL结果中排除某个范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆