假設下面的示例表:SQL JOIN表與子表的
+-----+-----+-----+
| ID1 | ID2 | VAL |
+-----+-----+-----+
| A | X | 1 |
| A | Y | 2 |
| A | Z | 3 |
| B | X | 4 |
| B | Z | 5 |
| C | Y | 6 |
+-----+-----+-----+
說我很感興趣,ID1 = B
並希望它和別人比較。期望的輸出將是:
+---------+---------+---------+---------+---------+---------+---------+
| REF.ID1 | OTH.ID1 | REF.ID2 | REF.ID2 | REF.VAL | OTH.VAL | DIFF |
+---------+---------+---------+---------+---------+---------+---------+
| B | A | X | X | 4 | 1 | 3 |
| B | A | NULL | Y | NULL | 2 | -2 |
| B | A | Z | Z | 5 | 3 | 2 |
| B | B | X | X | 4 | 4 | 0 |
| B | B | Z | Z | 5 | 5 | 0 |
| B | C | NULL | Y | NULL | 6 | -6 |
+---------+---------+---------+---------+---------+---------+---------+
謝謝。
是什麼問題? – 2013-02-18 14:23:00
我的問題是如何獲得所需的輸出。什麼可能不清楚? – 2013-02-18 14:27:41