我是SQL的新手,有些问题希望你能帮助我:
ORACLE 10g
表格帐户
+----------+----------+ | ACCOUNTID| LBKEY | +----------+----------+ | ... | ... | | 254 | value254 | | ... | ... | | 401 | value401 | | ... | ... | | 405 | value405 | +----------+----------+
交叉参考表
+----------+----------+----------+--------+ | IDTABLE2 | ACCOUNTID| OIDID | VALUE | +----------+----------+----------+--------+ | ... | ... | ... | ... | | 475 | 401 | 4 | 40000 | | 476 | 405 | 4 | 35000 | | ...| ... | ... | ... | | 3000 | 254 | 5 | PARIS | | 3001 | 401 | 5 | LONDON | | 3002 | 405 | 5 | SYDNEY | | ...| ... | ... | ... | +----------+----------+----------+--------+
表格OID
+----------+-------------+-------------+ | OIDID | OID | DESCRIPTION | +----------+-------------+-------------+ | 1 | x | x | | 2 | x | x | | 3 | x | x | | 4 | 1.3.6.1.4.1 | Post Code | | 5 | 1.3.6.1.4.2 | City | | 6 | x | x | | 7 | x | x | | 8 | x | x | | 9 | x | x | | 10 | x | x | +----------+-------------+-------------+
预期结果
约束:所有在交叉引用表中都有一个邮政编码(OID 4)或城市代码(OID 5)的ACCOUNT(LBKEY)
+----------+-------------+-------------+ | LBKEY | POST CODE | CITY | +----------+-------------+-------------+ | value254 | null | PARIS | | value401 | 40000 | LONDON | | value405 | 35000 | SYDNEY | +----------+-------------+-------------+
答案 0 :(得分:0)
我认为这对您有用:
select
lbkey,
cross_post.value as postcode,
cross_city.value as city
from
ACCOUNT a,
cross cross_city,
cross cross_post,
where
a.accountid=cross_city.accountid(+) and
a.accountid=cross_post.accountid(+) and
nvl(cross_city.oidid,5)=5 and
nvl(cross_post.oidid,4)=4 and
(cross_city.oidid is not null or cross_post.oidid is not null)
答案 1 :(得分:0)
三种不同的做法:
Oracle 11g R2架构设置:
CREATE TABLE ACCOUNT ( ACCOUNTID, LBKEY ) AS
SELECT 254, 'value254' FROM DUAL
UNION ALL SELECT 401, 'value401' FROM DUAL
UNION ALL SELECT 405, 'value405' FROM DUAL
UNION ALL SELECT 406, 'value406' FROM DUAL;
CREATE TABLE CrossReference ( IDTABLE2, ACCOUNTID, OIDID, VALUE ) AS
SELECT 475, 401, 4, '40000' FROM DUAL
UNION ALL SELECT 476, 405, 4, '35000' FROM DUAL
UNION ALL SELECT 3000, 254, 5, 'PARIS' FROM DUAL
UNION ALL SELECT 3001, 401, 5, 'LONDON' FROM DUAL
UNION ALL SELECT 3002, 405, 5, 'SYDNEY' FROM DUAL
UNION ALL SELECT 4000, 406, 6, 'x' FROM DUAL;
CREATE TABLE OID (OIDID, OID, DESCRIPTION ) AS
SELECT 1, 'x', 'x' FROM DUAL
UNION ALL SELECT 2, 'x', 'x' FROM DUAL
UNION ALL SELECT 3, 'x', 'x' FROM DUAL
UNION ALL SELECT 4, '1.3.6.1.4.1', 'Post Code' FROM DUAL
UNION ALL SELECT 5, '1.3.6.1.4.2', 'City' FROM DUAL
UNION ALL SELECT 6, 'x', 'x' FROM DUAL
UNION ALL SELECT 7, 'x', 'x' FROM DUAL
UNION ALL SELECT 8, 'x', 'x' FROM DUAL
UNION ALL SELECT 9, 'x', 'x' FROM DUAL
UNION ALL SELECT 10, 'x', 'x' FROM DUAL;
查询1 :
SELECT LBKEY,
MAX( CASE OIDID WHEN 4 THEN VALUE END ) AS "Post Code",
MAX( CASE OIDID WHEN 5 THEN VALUE END ) AS "City"
FROM ACCOUNT a
INNER JOIN
CrossReference c
ON ( a.ACCOUNTID = c.ACCOUNTID )
WHERE c.OIDID IN ( 4, 5 )
GROUP BY LBKEY
<强> Results 强>:
| LBKEY | POST CODE | CITY |
|----------|-----------|--------|
| value254 | (null) | PARIS |
| value405 | 35000 | SYDNEY |
| value401 | 40000 | LONDON |
查询2 :
WITH data AS (
SELECT LBKEY,
( SELECT VALUE
FROM CrossReference c
WHERE c.ACCOUNTID = a.ACCOUNTID
AND c.OIDID = 4 ) AS "Post Code",
( SELECT VALUE
FROM CrossReference c
WHERE c.ACCOUNTID = a.ACCOUNTID
AND c.OIDID = 5 ) AS "City"
FROM ACCOUNT a
)
SELECT *
FROM data
WHERE "Post Code" IS NOT NULL
OR "City" IS NOT NULL
<强> Results 强>:
| LBKEY | POST CODE | CITY |
|----------|-----------|--------|
| value254 | (null) | PARIS |
| value401 | 40000 | LONDON |
| value405 | 35000 | SYDNEY |
查询3 :
SELECT LBKEY,
c1.VALUE AS "Post Code",
c2.VALUE AS City
FROM ACCOUNT a
LEFT OUTER JOIN
( SELECT ACCOUNTID, VALUE FROM CrossReference WHERE OIDID = 4 ) c1
ON ( c1.ACCOUNTID = a.ACCOUNTID )
LEFT OUTER JOIN
( SELECT ACCOUNTID, VALUE FROM CrossReference WHERE OIDID = 5 ) c2
ON ( c2.ACCOUNTID = a.ACCOUNTID )
WHERE c1.VALUE IS NOT NULL
OR c2.VALUE IS NOT NULL
<强> Results 强>:
| LBKEY | POST CODE | CITY |
|----------|-----------|--------|
| value254 | (null) | PARIS |
| value401 | 40000 | LONDON |
| value405 | 35000 | SYDNEY |