在一个新的结果集中合并两个查询 [英] Merging two queries in a new resultset

查看:102
本文介绍了在一个新的结果集中合并两个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Table_Check:
sequence_no名称年龄性别Empno
1 A 11 M 123
2 B 12 F 456
3 C 13 M 457
4 D 14 F 123
5 E 15 M 121
6 F 16 F 112
7 G 17 M 110
8 H 18 F 124

因此,如果我将两个查询写为:
从Table_Check中选择名称作为NameT1,其中sequence_no%2<> 0;
其结果将是:
名称T1
A
C
E
G
从Table_Check中选择名称作为NameT2,其中sequence_no%2 = 0;
其结果将是:
名称T1
B
D
F
H
必需的结果是以上两个查询的结果,但在单个表中,如下所示:
NameT1 NameT2
A B
C D
E F
G H

因此通常可以通过编写两个不同的查询来实现.
现在如何用新的2列创建这个新表,在ist列中有query1的结果,在第二列中有查询II的结果..
急切地等待帮助..
谢谢..

Table_Check:
sequence_no Name Age Sex Empno
1 A 11 M 123
2 B 12 F 456
3 C 13 M 457
4 D 14 F 123
5 E 15 M 121
6 F 16 F 112
7 G 17 M 110
8 H 18 F 124

So if i write two queries as :
Select name as NameT1 from Table_Check where sequence_no%2<>0;
Its Resultant Will Be:
NameT1
A
C
E
G
Select name as NameT2 from Table_Check where sequence_no%2=0;
Its Resultant Will Be:
NameT1
B
D
F
H
Resultant required Is the resultant of above two queries but in a single table like this:
NameT1 NameT2
A B
C D
E F
G H

So usually how it can be done is writing two different queries.
Now how to create this new table with new 2 columns having result of query1 in ist column and result of query II in second column..
Eagerly waiting for help..
Thanks..

推荐答案

我可以提出一种解决方法,但是在我这样做之前,您能回答一下为什么要依赖sequence_no字段吗(看起来像一个很像自动编号的主键)来确定要在第1列和第2列中放置什么?

我问的原因是,您似乎想与其他人搭档"-当一个人进入时,您的顺序发生了什么?
I can suggest a way of doing it, but before I do so, can you answer why you are relying on the sequence_no field (that looks a lot like an auto-numbering primary key) to determine what to put in column 1 and 2?

The reason I ask is that it would appear that you are trying to "partner up" people - what happens to your sequence when there''s a single person entered?


我可以建议一种方法这样做,但在此之前,您能否回答为什么要依靠sequence_no字段(看起来很像自动编号的主键)来确定要在第1列和第2列中放置什么? 我问的原因是,您似乎想与其他人搭档"-当输入一个人时,您的顺序会怎样?


先生.达面..
准确地说,我需要将该结果表示为的报告应该有两列,并且该表的ist记录在报告的左列中,第二条记录在右列abd中,然后将第三条记录再次插入ist列并喜欢直到记录的末尾..当只有一个人输入时,将输入最多1个键.
并且此报告关联与特定雇员有关,我们在其中检查特定雇员的上载文件..
对于例如
ID ------ Empno ----- Desc ----文件名------ ref_no ----- seq_no
1 ------- 12345 ----- a.doc ---- ABC ----------- 01 --------- 1
1 ------- 12345 ----- b.doc ---- XYZ ----------- 02 --------- 2
1 ------- 12345 ----- d.doc ---- PQR ----------- 81 --------- 3
1 ------- 12345 ----- x.doc ---- CDE ----------- 11 --------- 4
1 ------- 67890 ----- a1.doc --- ABCD ---------- 011 -------- 1
1 ------- 67890 ----- b1.doc --- XYZD ---------- 021 -------- 2
1 ------- 67890 ----- d1.doc --- PQRD ---------- 811 -------- 3
1 ------- 67890 ----- x1.doc --- CDED ---------- 111 -------- 4


现在,当我在程序中制定此结果时,我只需要特定雇员的Desc列即可,例如-12345号雇员

Desc1 ---------- Desc2
a.doc ------------ b.doc
c.doc ------------ d.doc

而为什么我要使用sequence_no则是通过两个不同的查询将奇数行和偶数行分开,然后将它们显示为一个以上的单个表..

希望您能理解我的意思.
I can suggest a way of doing it, but before I do so, can you answer why you are relying on the sequence_no field (that looks a lot like an auto-numbering primary key) to determine what to put in column 1 and 2?
The reason I ask is that it would appear that you are trying to "partner up" people - what happens to your sequence when there''s a single person entered?


MR. DAMIAN..
Acctually the report i need to formulate this result into is supposed to have two columns and where in the ist record from this table goes into the left column of the report and second record goes into the right column abd then the third record is again inserted into the ist column and likewize till the end of records..And when there is only single person enterred that will be keyed upto 1.
And this report association is with a particular employee where in we check the uploaded files of a particular employee..
For e.g
ID------Empno-----Desc----Filename------ref_no-----seq_no
1-------12345-----a.doc----ABC-----------01---------1
1-------12345-----b.doc----XYZ-----------02---------2
1-------12345-----d.doc----PQR-----------81---------3
1-------12345-----x.doc----CDE-----------11---------4
1-------67890-----a1.doc---ABCD----------011--------1
1-------67890-----b1.doc---XYZD----------021--------2
1-------67890-----d1.doc---PQRD----------811--------3
1-------67890-----x1.doc---CDED----------111--------4


Now when i am formulating this result in my procedure i just need the Desc column for a particular employee for e.g employee no -12345

Desc1----------Desc2
a.doc----------b.doc
c.doc----------d.doc

And why i have used this sequence_no is to separate the odd and the even rows by two different queries and then display them into a single table as one above..

Hope you get my point.


我认为此查询有效:
I think this query works:
select NameT1 = o.name, NameT2 = e.name
from
(
    select *, t=(sequence_no-1)/2
    from Table_Check 
    where sequence_no%2<>0
) o inner join
(
    select *, t=(sequence_no-1)/2
    from Table_Check 
    where sequence_no%2=0
) e on e.t = o.t




但这不是一个好人!




But it is not a nice one!


这篇关于在一个新的结果集中合并两个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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