简体   繁体   English

从不同列中的多个表中获取值

[英]Fetching values from multiple tables in different columns

I need to fetch data from multiple tables but with some scenarios here are test scripts to re-create the problem 我需要从多个表中获取数据,但是在某些情况下,这里有一些测试脚本可以重新创建问题

create table sub_test (sub_id number);

create table sub_svc_test (sub_id number, sub_svc_id number);

create table sub_svc_parm_test (sub_svc_id number, parm_id number, val varchar2(20) );

insert into sub_test values (100);
insert into sub_test values (101);
insert into sub_test values (102);

insert into sub_svc_test values (100,1001);
insert into sub_svc_test values (100,1002);
insert into sub_svc_test values (101,1005);
insert into sub_svc_test values (101,1006);
insert into sub_svc_test values (101,1007);
insert into sub_svc_test values (102,1009);
insert into sub_svc_test values (102,1010);

insert into sub_svc_parm_test values (1001, 51, 'test_id');
insert into sub_svc_parm_test values (1001, 53, 'no');
insert into sub_svc_parm_test values (1002, 54, 'max');
insert into sub_svc_parm_test values (1005, 51, 'test_id');
insert into sub_svc_parm_test values (1007, 51, 'test_id');
insert into sub_svc_parm_test values (1007, 54, 'min');

I need to fetch values from sub_svc_parm_test table for VAL column for a particular parm_id such that 我需要从sub_svc_parm_test表中为特定parm_id的VAL列获取值,以便

select * from sub_svc_test ss, sub_svc_parm_test ssp
where ss.sub_svc_id = ssp.sub_svc_id and parm_id = 51;

this query will give me the VAL for 51 parm_id now i need to craete a view which will show me the VAL for parm_id 51, 54 but in differnet column like 此查询将为我提供51 parm_id的VAL,现在我需要创建一个视图,该视图将向我显示parm_id 51、54的VAL,但在differnet列中

select ssp.val, ssp1.val
from sub_svc_test ss, sub_svc_parm_test ssp, sub_svc_test ss1,
     sub_svc_parm_test ssp1
where ss.sub_svc_id = ssp.sub_svc_id
  and ssp.parm_id = 51
  and ssp1.parm_id  = 54
  and ss1.sub_svc_id = ssp1.sub_svc_id;

This query will give me output but also it performs cross joins as i have not join the sub_svc_test ss, and sub_svc_test ss1 so it gives me 6 rows 2*3 but the requirement is that it should show me MAX rows of any column in our case it is first column (3 rows) and the remaining row which do not have data can contain any string or simply null in it like 该查询将为我提供输出,但是由于我还没有加入sub_svc_test ss和sub_svc_test ss1,所以它执行交叉联接,因此它给了我6行2 * 3,但要求是在我们的情况下应该向我显示任何列的MAX行它是第一列(3行),其余没有数据的行可以包含任何字符串或仅包含null

VAL             VAL_1
--------------     -------------             
test_id         max
test_id         min
test_id         null

i am using ---- Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production 我正在使用---- Oracle Database 11g企业版11.2.0.3.0版-64位生产

Please ask for any clarification 请要求任何澄清

Thanks 谢谢

So you actually want to perform two separate selects and display them next to each other. 因此,您实际上要执行两个单独的选择,并将它们彼此相邻显示。

Maybe something like this could work: 也许这样的事情可能会起作用:

select val, val1 from (
  (select rownum as r, val as val from sub_svc_parm_test ssp1 where parm_id = 51) 
  full outer join 
  (select rownum as r1, val as val1 from sub_svc_parm_test ssp2 where parm_id = 54)
  on r = r1
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM