-4
SELECT TBL1.C1, TBL1.C2, TBL2.C3, TBL2.C4 
    FROM(
        SELECT 100 C1, 'ABC' C2
        UNION
        SELECT 101 C1, 'XYZ' C2
        UNION 
        SELECT 102 C1, 'PQR' C2 
        UNION
        SELECT 103 C1, 'MNO' C2
        ) TBL1,
        (
        SELECT 1 C3, 'Saving Deposits' C4
        UNION
        SELECT 2 C3, 'Current Deposits' C4
        UNION
        SELECT 3 C3, 'Term Deposits' C4
        )TBL2

上面的查询给出以下结果

--------------------------------------------
C1  |  C2 |C3 |     C4
--------------------------------------------
100 | ABC | 1 | Saving Deposits
101 | XYZ | 1 | Saving Deposits
100 | ABC | 2 | Current Deposits
101 | XYZ | 2 | Current Deposits
100 | ABC | 3 | Term Deposits
101 | XYZ | 3 | Term Deposits

但我期待如下结果

--------------------------------------------
C1  |  C2 |C3 |     C4
--------------------------------------------
100 | ABC | 1 |  Saving Deposits
100 | ABC | 2 | Current Deposits
100 | ABC | 3 | Term Deposits
101 | XYZ | 1 | Saving Deposits
101 | XYZ | 2 | Current Deposits
101 | XYZ | 3 | Term Deposits

请帮我 ....

4

3 回答 3

5

ORDER BY在查询末尾添加:

ORDER BY TBL1.C1, TBL1.C2, TBL2.C3

所以你的整个查询应该是:

SELECT TBL1.C1, TBL1.C2, TBL2.C3, TBL2.C4 
    FROM(
        SELECT 100 C1, 'ABC' C2
        UNION
        SELECT 101 C1, 'XYZ' C2
        UNION 
        SELECT 102 C1, 'PQR' C2 
        UNION
        SELECT 103 C1, 'MNO' C2
        ) TBL1,
        (
        SELECT 1 C3, 'Saving Deposits' C4
        UNION
        SELECT 2 C3, 'Current Deposits' C4
        UNION
        SELECT 3 C3, 'Term Deposits' C4
        )TBL2
    ORDER BY TBL1.C1, TBL1.C2, TBL2.C3
于 2013-07-12T07:38:24.060 回答
4

尝试这个

SELECT TBL1.C1, TBL1.C2, TBL2.C3, TBL2.C4 
    FROM(
        SELECT 100 C1, 'ABC' C2
        UNION
        SELECT 101 C1, 'XYZ' C2
        UNION 
        SELECT 102 C1, 'PQR' C2 
        UNION
        SELECT 103 C1, 'MNO' C2
        ) TBL1,
        (
        SELECT 1 C3, 'Saving Deposits' C4
        UNION
        SELECT 2 C3, 'Current Deposits' C4
        UNION
        SELECT 3 C3, 'Term Deposits' C4
        )TBL2 
Order by TBL1.C2
于 2013-07-12T07:39:27.040 回答
2

您可以使用 Order by 主要按 c2 升序排列输出,然后按 c3 升序排列输出。

于 2013-07-12T07:42:36.307 回答