存储过程中的高级选择
我得到了这个表:
CREATE TABLE Test_Table (
old_val VARCHAR2(3),
new_val VARCHAR2(3),
Updflag NUMBER,
WorkNo NUMBER );
这在我的表中:
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('1',' 20',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('2',' 20',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('2',' 30',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('3',' 30',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('4',' 40',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('4',' 40',0,0);
现在我的表看起来像这样:(
Row Old_val New_val Updflag WorkNo
1 '1' ' 20' 0 0
2 '2' ' 20' 0 0
3 '2' ' 30' 0 0
4 '3' ' 30' 0 0
5 '4' ' 40' 0 0
6 '5' ' 40' 0 0
如果 new_val
列中的值相同,那么它们在一起,并且相同的内容转到 old_val< /代码>) 所以在上面的例子中,第 1-4 行在一起,第 5-6 行
现在在我的存储过程中有一个游标:
SELECT t1.Old_val, t1.New_val, t1.updflag, t1.WorkNo
FROM Test_Table t1
WHERE t1.New_val =
(
SELECT t2.New_val
FROM Test_Table t2
WHERE t2.Updflag = 0
AND t2.Worknr = 0
AND ROWNUM = 1
)
输出是这样的:
Row Old_val New_val Updflag WorkNo
1 1 20 0 0
2 2 20 0 0
我的问题是,我不知道如何通过一次选择获取第 1 行到第 4 行。 (我有一个想法,有 4 个子查询,但如果更多数据匹配在一起,这将不起作用)
你们有人有想法吗?
i got this Table:
CREATE TABLE Test_Table (
old_val VARCHAR2(3),
new_val VARCHAR2(3),
Updflag NUMBER,
WorkNo NUMBER );
and this is in my Table:
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('1',' 20',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('2',' 20',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('2',' 30',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('3',' 30',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('4',' 40',0,0);
INSERT INTO Test_Table (old_val, new_val, Updflag , WorkNo) VALUES('4',' 40',0,0);
now my Table Looks like this:
Row Old_val New_val Updflag WorkNo
1 '1' ' 20' 0 0
2 '2' ' 20' 0 0
3 '2' ' 30' 0 0
4 '3' ' 30' 0 0
5 '4' ' 40' 0 0
6 '5' ' 40' 0 0
(if the value in the new_val
column are same then they are together and the same goes to old_val
)
so in the example above row 1-4 are together and row 5-6
at the moment i have in my Stored Procedure a cursor:
SELECT t1.Old_val, t1.New_val, t1.updflag, t1.WorkNo
FROM Test_Table t1
WHERE t1.New_val =
(
SELECT t2.New_val
FROM Test_Table t2
WHERE t2.Updflag = 0
AND t2.Worknr = 0
AND ROWNUM = 1
)
the output is this:
Row Old_val New_val Updflag WorkNo
1 1 20 0 0
2 2 20 0 0
my Problem is, i dont know how to get row 1 to 4 with one select.
(i had an idea with 4 sub-querys but this wont work if its more data that matches together)
does anyone of you have an idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用分析来定义连续行组:
当当前行与前一行不在同一组中时,内部 SELECT 会构建一个等于 1 的“GAP”列。
外部 SELECT 使用间隙列上的运行总计来获取组编号。
编辑 2
由于分析函数的原因,您无法将 FOR UPDATE 子句直接添加到查询中。但是,您可以直接查询基表:
you can use analytics to define groups of contiguous rows:
The inner SELECT builds a "GAP" column that equals 1 when the current row is not in the same group as the preceeding.
The outer SELECT uses a running total over the gap column to get the group number.
Edit 2
You can't add the FOR UPDATE clause directly to the query because of the analytic functions. You can query the base table directly however:
如果您想要的是返回“与某些内容在一起”的所有行,那么原始示例数据似乎不会提供不应返回的行。因此,让我们添加以下内容:
该行不应与任何内容放在一起,也不应返回。鉴于此,我认为我们可以使用 EXISTS 来得到你想要的:
If what you want is all rows that "are together with something" returned, then it does not appear that your original sample data provides a row that shouldn't be returned. So, let's add the following:
This row shouldn't be together with anything and shouldn't be returned. Given that, I think we can use an EXISTS to get what you want: