简体   繁体   中英

What is the correct MS Access SQL syntax for creating a union query

The Microsoft documentation states:

CREATE VIEW view [(field1[, field2[, ...]])] AS selectstatement

and lists a number of provisions. I am trying to create a union query with the CREATE VIEW statement using ADODB . The union itself I can run in Access (2013) and save (it shows it is saved as a union query with the two little interlocked circles icon). But the Microsoft.ACE.OLEDB.12.0 engine complains either Unions not allowed in a subquery or Only simple SELECT queries are allowed in VIEWS . Do I need to use DAO or is there a different syntax? My create view statement is:

uString = "CREATE VIEW vwSpendingTotals (Spending, Name) as SELECT Spending, Name FROM " & _
" (SELECT  Sum( tblA.Amount) as Spending, 'Auto' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_AUTO as tblA on gl.Trans_ID = tblA.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1)  Union " & _
" SELECT  Sum( tblH.Amount) as Spending, 'Household' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Household as tblH on gl.Trans_ID = tblH.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1) union " & _
" SELECT  Sum( tblL.Amount) as Spending, 'Living' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Living as tblL on gl.Trans_ID = tblL.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1)  union " & _
" SELECT  Sum( tblM.Amount) as Spending, 'Medical' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Medical as tblM on gl.Trans_ID = tblM.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1) union " & _
" SELECT  Sum( tblP.Amount) as Spending, 'Personal' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Personal as tblP on gl.Trans_ID = tblP.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1) union " & _
" SELECT  Sum( tblU.Amount) as Spending, 'Utilities' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Utilities as tblU on gl.Trans_ID = tblU.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1)) order by Name"
objConnection.Execute uString

objConnection has been instantiated a little earlier in the script. Should the Microsoft documentation be updated to reflect the error being returned by the Jet 12 engine or am I barking up the wrong tree?

Thanks in advance, Harold

Have you run it with an alias after your subquery:

uString = "CREATE VIEW vwSpendingTotals (Spending, Name) as SELECT Spending, Name FROM " & _
" (SELECT  Sum( tblA.Amount) as Spending, 'Auto' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_AUTO as tblA on gl.Trans_ID = tblA.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1)  Union " & _
" SELECT  Sum( tblH.Amount) as Spending, 'Household' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Household as tblH on gl.Trans_ID = tblH.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1) union " & _
" SELECT  Sum( tblL.Amount) as Spending, 'Living' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Living as tblL on gl.Trans_ID = tblL.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1)  union " & _
" SELECT  Sum( tblM.Amount) as Spending, 'Medical' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Medical as tblM on gl.Trans_ID = tblM.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1) union " & _
" SELECT  Sum( tblP.Amount) as Spending, 'Personal' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Personal as tblP on gl.Trans_ID = tblP.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1) union " & _
" SELECT  Sum( tblU.Amount) as Spending, 'Utilities' as Name from tblGeneral_Ledger  gl " & _
" inner join tblAccount_Utilities as tblU on gl.Trans_ID = tblU.Trans_ID " & _
" where gl.Debit_Credit < 0 AND Trans_Date > DateSerial(Year(Now()),1,1))a order by Name"
objConnection.Execute uString

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM