ヘッダー行と詳細行を 1 つの結果セットに結合する必要があります。
(サンプル DDL 以降inserts
):
注文:
OrderID OrderDate CurrencyID BuyAmount BuyRate
======= ======================= ========== ========= ========
1 2011-09-01 15:57:00.000 7 12173.60 1.243893
1 2011-09-01 15:57:00.000 9 69.48 1
注文の詳細:
OrderID CurrencyID SellAmount SellRate
======= ========== ========== ========
1 7 10000 1
1 8 12384 0.9638
私は彼らが参加しOrderID
たいCurrencyID
:
OrderID CurrencyID BuyAmount BuyRate SellAmount SellRate
======= ========== ========= ======== ========== ========
1 7 12173.60 1.243893 10000 1
1 8 NULL NULL 12384 0.9638
1 9 69.48 1 NULL NULL
サンプル スクリプト
--USE Scratch
--Create a temporary `Orders` and, `OrderDetails` tables:
IF OBJECT_ID('tempdb..#Orders') > 0 DROP TABLE #Orders
CREATE TABLE #Orders
(
OrderID int NOT NULL,
OrderDate datetime NOT NULL,
CurrencyID int NOT NULL,
BuyAmount money NOT NULL,
BuyRate real NOT NULL
)
IF OBJECT_ID('tempdb..#OrderDetails') > 0 DROP TABLE #OrderDetails
CREATE TABLE #OrderDetails
(
OrderID int NOT NULL,
CurrencyID int NOT NULL,
SellAmount money NOT NULL,
SellRate real NOT NULL
)
-- **Insert sample data:**
INSERT INTO #Orders (OrderID, OrderDate, CurrencyID, BuyAmount, BuyRate)
VALUES (1, '20110901 15:57:00', 7, 12173.60, 1.2438933)
INSERT INTO #Orders (OrderID, OrderDate, CurrencyID, BuyAmount, BuyRate)
VALUES (1, '20110901 15:57:00', 9, 69.48, 1)
INSERT INTO #OrderDetails (OrderID, CurrencyID, SellAmount, SellRate)
VALUES (1, 7, 10000, 1)
INSERT INTO #OrderDetails (OrderID, CurrencyID, SellAmount, SellRate)
VALUES (1, 8, 12384, 0.9638)
/*Desired Output:
OrderID CurrencyID BuyAmount BuyRate SellAmount SellRate
======= ========== ========= ======== ========== ========
1 7 12173.60 1.243893 10000 1
1 8 NULL NULL 12384 0.9638
1 9 69.48 1 NULL NULL
*/
目的の出力を生成できるRIGHT OUTER JOIN
、FULL OUTER JOIN
、の組み合わせが見つかりません。COALESCE
アップデート:
テーブルからのOrderDetails
一致が含まれていない可能性もあります。CurrencyID
Orders
注文:
OrderID CurrencyID BuyAmount BuyRate
======= ========== ========= ========
1 7 12173.60 1.243893
1 9 69.48 1
注文の詳細:
OrderID CurrencyID SellAmount SellRate
======= ========== ========== ========
1 8 12384 0.9638