2013-12-23 54 views
1

我在SQL新手和有問題希望你能幫助我:的Oracle SQL場

的Oracle 10g

表ACCOUNT

 
+----------+----------+ 
| 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   | 
+----------+-------------+-------------+ 

預期結果

約束:所有ACCOUNT(LBKEY)誰擁有交叉引用表

一個郵政編碼(OID 4)或城市代碼(OID 5)
 
+----------+-------------+-------------+ 
| LBKEY | POST CODE | CITY  | 
+----------+-------------+-------------+ 
| value254 | null  | PARIS  | 
| value401 | 40000  | LONDON  | 
| value405 | 35000  | SYDNEY  | 
+----------+-------------+-------------+ 

回答

0

三種不同的方式:

SQL Fiddle

的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 | 
+0

它就像一個魅力!非常感謝並祝聖誕快樂:) – juhnz

+0

不是問題,對你也是聖誕快樂 - 如果這個(或者其他答案)回答了你的問題,那麼請接受它並/或者upvote(如果你有這樣的聲望)。 – MT0

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) 
這樣做的
+0

請採取任何罪行,但爲什麼不使用ANSI SQL連接21世紀?那些專有連接真的是老派和神祕的,尤其對於新手來說更難理解。 –

+0

我didnt'work工作: 00903. 00000 - 「無效的表名」 交叉cross_city, 交叉cross_post, 是語法正確的?因爲我沒有名爲「cross_city」或「cross_post」的表(表:account,oid,crossreference) – juhnz

+0

我假設你的表名是交叉的。你應該用十字參考表的真實姓名替換十字。 –