在下面的查询中,我使用Subquery1和Subquery2来获取帐号和帐户名。但是,除了子查询2中的附加表account_nameinfo_t之外,第一个子查询和第二个子表连接相同的表以获取帐户名。有没有办法我避免从其他表中选择并只使用子查询1的值,即帐号#来获取子查询2中的帐户名称?
SELECT
(
SELECT acct.account_no
FROM group_t grp1, account_t acct
WHERE grp1.poid_id0 = grpbm.obj_id0
AND acct.poid_id0 = grp1.ACCOUNT_OBJ_ID0
) PARENT_ACCOUNT, -- (#Subquery 1 to get the parent account)
(
SELECT ant.Firstname || ' ' || ant.LastName
FROM group_t grp1, account_t acct,account_nameinfo_t ant
WHERE grp1.poid_id0 = grpbm.obj_id0
AND acct.poid_id0 = grp1.ACCOUNT_OBJ_ID0
AND ant.obj_id0 = acct.poid_id0
) "ACCOUNT NAME", -- (#Subquery 2 to get the parent account name which is in a different table)
bgs.REC_ID2 RECORD_TYPE,
bgs.current_bal VALUE
FROM group_t grp,
group_billing_members_t grpbm,
BAL_GRP_SUB_BALS_T bgs
WHERE poid_type = '/group/sharing/discounts'
AND grpbm.OBJECT_ID0 = grp.ACCOUNT_OBJ_ID0
AND bgs.obj_id0 = grp.BAL_GRP_OBJ_ID0
AND bgs.rec_id2 NOT IN (1000203,
1030001,
1000303,
1000306)
ORDER BY PARENT_ACCOUNT;
答案 0 :(得分:0)
Oracle支持您可能认为有用的WITH
子句:http://psoug.org/reference/with.html
基本上,它允许您在查询中创建可以多次访问的临时视图。在您的情况下,您的公共连接的结果可以“分解”,结果可以重复使用。
答案 1 :(得分:0)
正如ALEXPOOL建议的那样,我尝试使用ANSI连接并且它可以工作。欢迎任何好处。
SELECT C1."PARENT ACCOUNT",C1."ACCOUNT NAME", A1.RECORD_TYPE, A1.CURRENT_BAL
FROM
(SELECT
bgs.REC_ID2 RECORD_TYPE,
bgs.current_bal,grpbm.OBJ_ID0
FROM group_t grp ,
group_billing_members_t grpbm,
BAL_GRP_SUB_BALS_T bgs
WHERE
poid_type='/group/sharing/discounts' and
grpbm.OBJECT_ID0 = grp.ACCOUNT_OBJ_ID0 and
bgs.obj_id0 = grp.BAL_GRP_OBJ_ID0 and
bgs.rec_id2 not in ( 1000203,
1030001,
1000303,
1000306) ) A1 JOIN
( SELECT grp1.ACCOUNT_OBJ_ID0,grp1.POID_ID0
FROM group_t grp1
) B1 ON (B1.poid_id0 = A1.OBJ_ID0)
JOIN
( SELECT acct.account_no "PARENT ACCOUNT",
ant.FIRST_NAME||' '||ant.LAST_NAME "ACCOUNT NAME",
acct.poid_id0
FROM
account_t acct,
account_nameinfo_t ant
WHERE acct.poid_id0 = ant.obj_id0 and
ant.rec_id=1) C1 ON (C1.poid_id0 = B1.ACCOUNT_OBJ_ID0)
order by C1."PARENT ACCOUNT";
答案 2 :(得分:0)
看起来您可以使用简单连接而不是子查询来简化此操作,无论是在选择列表中还是作为内联视图:
// Special case for the head.
if (elementToDelete == *head) {
*head = element->next;
free(elementToDelete);
return true;
}
您似乎只在SELECT acct.account_no AS "PARENT ACCOUNT",
ant.first_name||' '||ant.last_name AS "ACCOUNT NAME",
bgs.rec_id2 AS record_type,
bgs.current_bal
FROM group_t grp
JOIN group_billing_members_t grpbm ON grpbm.obj_id0 = grp.account_obj_id0
JOIN group_t grp1 ON grp1.poid_id0 = grpbm.obj_id0
JOIN bal_grp_sub_bals_t bgs ON bgs.obj_id0 = grp.bal_grp_obj_id0
JOIN account_t acct ON acct.poid_id0 = grp1.account_obj_id0
JOIN account_nameinfo_t ant ON ant.obj_id0 = acct.poid_id0
WHERE grp.poid_type='/group/sharing/discounts'
AND bgs.rec_id2 not in (1000203, 1030001, 1000303, 1000306)
AND ant.rec_id = 1
ORDER BY "PARENT ACCOUNT";
的两个引用之间使用group_billing_members_t
,并且不清楚它们是否都指向同一记录,或者是否扩展为多行。列名似乎有些不一致,这可能来自您重新输入代码而不是复制和粘贴代码。如果它是相同的记录,那么您似乎能够删除该表并重新加入:
group_t
没有表格结构,关系,样本数据和预期结果,但相当推测。