2009-11-27 37 views
0

我有代碼,只會讓我選擇22列,然後用「太多的行延續」來了,如果我嘗試添加另一個。SQL選擇錯誤:太多的連接行

我有大約100列增加對這一公式,並需要對另外100這並不需要「檢查」複製。

代碼

strSQL = "SELECT a.[A], " _ & "(SELECT Max([Serial]) FROM [Data$] b WHERE b.[A]=a.A) As Serial, " _  
& "(SELECT Max([B]) FROM [Data$] b WHERE b.[A]=a.A) As B, " _  
& "(SELECT Max([C]) FROM [Data$] b WHERE b.[A]=a.A) As C, " _  
& "(SELECT Max([D]) FROM [Data$] b WHERE b.[A]=a.A) As D, " _  
& "(SELECT Max([E]) FROM [Data$] b WHERE b.[A]=a.A) As E, " _  
& "(SELECT Max([F]) FROM [Data$] b WHERE b.[A]=a.A) As F, " _  
& "(SELECT Max([G]) FROM [Data$] b WHERE b.[A]=a.A) As G, " _  
& "(SELECT Max([H]) FROM [Data$] b WHERE b.[A]=a.A) As H, " _  
& "(SELECT Max([I]) FROM [Data$] b WHERE b.[A]=a.A) As I, " _  
& "(SELECT Max([J]) FROM [Data$] b WHERE b.[A]=a.A) As J, " _  
& "(SELECT Max([K]) FROM [Data$] b WHERE b.[A]=a.A) As K, " _  
& "(SELECT Max([L]) FROM [Data$] b WHERE b.[A]=a.A) As L, " _  
& "(SELECT Max([M]) FROM [Data$] b WHERE b.[A]=a.A) As M, " _  
& "(SELECT Max([N]) FROM [Data$] b WHERE b.[A]=a.A) As N, " _  
& "(SELECT Max([O]) FROM [Data$] b WHERE b.[A]=a.A) As O, " _  
& "(SELECT Max([P]) FROM [Data$] b WHERE b.[A]=a.A) As P, " _  
& "(SELECT Max([Q]) FROM [Data$] b WHERE b.[A]=a.A) As Q, " _  
& "(SELECT Max([R]) FROM [Data$] b WHERE b.[A]=a.A) As R, " _  
& "(SELECT Max([S]) FROM [Data$] b WHERE b.[A]=a.A) As S, " _  
& "(SELECT Max([T]) FROM [Data$] b WHERE b.[A]=a.A) As T, " _  
& "(SELECT Max([U]) FROM [Data$] b WHERE b.[A]=a.A) As U, " _  
& "(SELECT Max([V]) FROM [Data$] b WHERE b.[A]=a.A) As V " _  
& "FROM [Data$] a " _  
& "GROUP BY a.[A]" 
+0

顯示整個宏代碼,所以我們可以看到你在做什麼。 – 2009-11-27 09:59:48

回答

0

那麼,就讓它少行,每行的時間長一點......

strSQL = "SELECT a.[A], " _ & "(SELECT Max([Serial]) FROM [Data$] b WHERE b.[A]=a.A) As Serial, " _  
& "(SELECT Max([B]) FROM [Data$] b WHERE b.[A]=a.A) As B, (SELECT Max([C]) FROM [Data$] b WHERE b.[A]=a.A) As C, (SELECT Max([D]) FROM [Data$] b WHERE b.[A]=a.A) As D, " _  
& "(SELECT Max([E]) FROM [Data$] b WHERE b.[A]=a.A) As E, (SELECT Max([F]) FROM [Data$] b WHERE b.[A]=a.A) As F, (SELECT Max([G]) FROM [Data$] b WHERE b.[A]=a.A) As G, " _  
& "(SELECT Max([H]) FROM [Data$] b WHERE b.[A]=a.A) As H, (SELECT Max([I]) FROM [Data$] b WHERE b.[A]=a.A) As I, (SELECT Max([J]) FROM [Data$] b WHERE b.[A]=a.A) As J, " _  
& "(SELECT Max([K]) FROM [Data$] b WHERE b.[A]=a.A) As K, (SELECT Max([L]) FROM [Data$] b WHERE b.[A]=a.A) As L, (SELECT Max([M]) FROM [Data$] b WHERE b.[A]=a.A) As M, " _  
& "(SELECT Max([N]) FROM [Data$] b WHERE b.[A]=a.A) As N, (SELECT Max([O]) FROM [Data$] b WHERE b.[A]=a.A) As O, (SELECT Max([P]) FROM [Data$] b WHERE b.[A]=a.A) As P, " _  
& "(SELECT Max([Q]) FROM [Data$] b WHERE b.[A]=a.A) As Q, (SELECT Max([R]) FROM [Data$] b WHERE b.[A]=a.A) As R, (SELECT Max([S]) FROM [Data$] b WHERE b.[A]=a.A) As S, " _  
& "(SELECT Max([T]) FROM [Data$] b WHERE b.[A]=a.A) As T, (SELECT Max([U]) FROM [Data$] b WHERE b.[A]=a.A) As U, (SELECT Max([V]) FROM [Data$] b WHERE b.[A]=a.A) As V " _  
& "FROM [Data$] a " _  
& "GROUP BY a.[A]" 
+0

太棒了!謝謝! – 2009-11-27 10:18:37

+0

如果你覺得我的回答是輝煌的,有上左側的三角形圖標,數字上面......你甚至可以選擇勾選「接受」圖標......(雖然astanders回答也很不錯) – 2009-11-27 10:21:56

+0

這是一種享受,並已節省了許多小時!天才。 Ta, – 2009-11-27 10:30:15

4

或者使

strSql = "bla bla" 
strSql = strSql & "bla bla 2" 
strSql = strSql & "bla bla 3" 
strSql = strSql & "bla bla 4" 
... 
strSql = strSql & "bla bla n" 

這將保持它的語句可重複使用並且不會跨越許多文本列。

+0

感謝您的幫助。以下答案正是我需要的。鉭, – 2009-11-27 10:30:51