如何在SQL Server中检查两个Coulmn值 [英] How to check two coulmn values in sql server

查看:77
本文介绍了如何在SQL Server中检查两个Coulmn值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的表:

col1 | col2
---- | ---
a .... | .e
b .... | .f
c .... | .g
d .... | .h


我想检查col2中的项目,col1中的下一行项目
即col2中的项目"e"等于col1中的项目"b".
在sql server中可能吗?

I have a table like this:

col1|col2
----| ---
a....|.e
b....|.f
c....|.g
d....|.h


i want to check that item in col2 next row item in col1
ie, item ''e'' in col2 eqals to item ''b'' in col1.
Is it possible in sql server???

推荐答案

我有一个解决方案,但只有在您也有序列号列的情况下才有可能:

让我告诉你我做了什么

表格:
I have a solution but it is only possible if you have a column for sequence number too:

let me tell you what i did

the table:
a --------- b --------- seq
-----------------------------
a --------- b --------- 1
b --------- b --------- 2
c --------- b --------- 3
d --------- e --------- 4
e --------- f --------- 5
f --------- b --------- 6
g --------- b --------- 7
h --------- i --------- 8
i --------- a --------- 9



列名以粗体显示.正常字体和带下划线的值的数据很可能会得出正确的答案.

现在,对于此表,以下查询将为您提供所需的结果;



column name shown in bold. data in normal font and underlined values are possible candidate to come out in correct answer.

Now for this table the following query will give you the desired results;

with tempTable as
(
select t1.a as val, ROW_NUMBER() over (order by t1.seq) as newRowId from table_1 t1, table_1 t2 where (t1.seq - t2.seq) = 1
)

select distinct t3.b from Table_1 t3, tempTable T where T.newRowId = t3.seq and t3.b = val



结果将是



the result will be

b
e
f
i




希望您能理解我的所作所为,希望该查询对您有所帮助.




I hope you understood what I did and hopefully the query will help.


这篇关于如何在SQL Server中检查两个Coulmn值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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