在SQL Server中检索父子记录

时间:2016-02-08 05:36:06

标签: sql parent-child master-detail

我有两张表TradeTrade Details,如下所示:

交易表:

ID    Portfolio   Source   Version     Createdon   Status
---------------------------------------------------------
1     Test1       RIM      1.0         2016-01-20    1
2     Test2       iOS      1.0         2016-01-20    1

交易明细表:

ID   TradeID    Ticker   Company     Shares     Action  Comments
----------------------------------------------------------------
1      1        MSFT     Microsoft     100        Buy      Test
2      1        IBM      IBM           200        Sell     Test
3      2        Yahoo    Yahoo Inc     50         Sell     Test
4      2        Goog     Google Inc    500        Buy      Test

我想以下列格式检索和显示数据。

输出

1     Test1       RIM      1.0         2016-01-20    1
1      1        MSFT     Microsoft     100        Buy      Test
2      1        IBM      IBM           200        Sell     Test
2     Test2       iOS      1.0         2016-01-20    1
3      2        Yahoo    Yahoo Inc     50         Sell     Test
4      2        Goog     Google Inc    500        Buy      Test     

1 个答案:

答案 0 :(得分:2)

您需要在两个表之间使用union,并添加自定义的其他列,以设置所需的顺序以生成所需的输出

ex [在oracle语法中,但想法是相同的]

RecBuff:array[0..9999] of AnsiChar;

这将生成以下内容:

    SELECT 
        CASE WHEN OID = 0 THEN TO_CHAR(P_TradeID) ELSE Portfolio END AS TradeID, Portfolio, Ticker, Company,SHARES, Action, Comments
    FROM
    (
        SELECT
            ID AS OID, 1 AS SOID,
            TradeID AS P_TradeID,
            TO_CHAR(ID) AS Portfolio, -- important to change to datatype of columns to be matching in the union in order for the union statement to work
            Ticker,
            Company,
            TO_CHAR(SHARES) SHARES,
            TO_CHAR(Action) Action,
            Comments
        FROM
        (
            --here will be your select statement.... the below is just a demo
            SELECT 1 AS ID, 1 AS TradeID, 'MSFT'  AS Ticker, 'Microsoft'  AS Company, 100 AS SHARES, 'Buy'  AS Action, 'Test' AS Comments FROM DUAL UNION
            SELECT 2 AS ID, 1 AS TradeID, 'IBM'   AS Ticker, 'IBM'        AS Company, 200 AS SHARES, 'Sell' AS Action, 'Test' AS Comments FROM DUAL UNION
            SELECT 3 AS ID, 2 AS TradeID, 'Yahoo' AS Ticker, 'Yahoo Inc'  AS Company, 50  AS SHARES, 'Sell' AS Action, 'Test' AS Comments FROM DUAL UNION
            SELECT 4 AS ID, 2 AS TradeID, 'Goog'  AS Ticker, 'Google Inc' AS Company, 500 AS SHARES, 'Buy'  AS Action, 'Test' AS Comments FROM DUAL
        )

        UNION --the trick is with the union here

        SELECT
            0 AS OID,0 AS SOID, --and the order columns here and in the above query
            ID AS P_TradeID,
            Portfolio,
            Source AS Ticker,
            Version AS Company,
            TO_CHAR(Createdon) AS SHARES,
            TO_CHAR(Status) AS Action,
            '' AS Comments
        FROM
        (
            --and again, make sure the datatype of the columns are the same!
            SELECT 1 AS ID, 'Test1' AS Portfolio, 'RIM' Source, '1.0' AS Version, '2016-01-20' AS Createdon, 1 AS Status  FROM DUAL UNION
            SELECT 2 AS ID, 'Test2' AS Portfolio, 'iOS' Source, '1.0' AS Version, '2016-01-20' AS Createdon, 1 AS Status  FROM DUAL
        )
    )
    ORDER BY 
        P_TradeId, SOID, OID

请参阅this SQL Fiddle link了解sql-server-2008语法