我在這裏失去我的觸摸。在過去,我會想出一個超級T-SQL查詢,我將如何創建此T-SQL子查詢?
Select t1.Number, t1.TransactionType, t1.Description, t1.Vendor, (Select max(t2.BatchId) From table2 t2 Where t1.Number=t2.Number and t1.TransactionType=t2.TransactionType Group By t2.number,t2.transactiontype) As BatchId From table1 t1
我需要table2的第二列。列被稱爲「結果」。
實施例:
table1: Number, TransactionType, Description, Vendor 1, Type1, Test1, Vendor1 2, Type1, Test2, Vendor2 1, Type2, Test3, Vendor3 3, Type2, Test1, Vendor2 table2: Number, TransactionType, BatchId, Result 1, Type1, 12, error1 1, Type1, 4, error2 1, Type2, 8, success 3, Type2, 7, success wanted ResultSet: Number, TransactionType, Description, Vendor, BatchId, Result 1, Type1, Test1, Vendor1, 12, error2 2, Type1, Test2, Vendor2, null, null 1, Type2, Test3, Vendor3, 8,success 3, Type2, Test1, Vendor2, 7,success
張貼的查詢採用所述第一5列的護理。那最後一欄呢?
將所需的表列添加到您的子查詢SELECT語句中。 IE:(選擇max(t2.BatchId),結果來自table2 t2 ... – blearn
子查詢在此上下文中可能不會返回多個列 –