我无法通过DAO在VBA脚本中运行此(通常是笨重的)SQL查询。它一直给我运行时错误3078,说“Microsoft Jet数据库引擎无法找到输入表或查询'SELECT Hist2.DateM ... GROUP BY Hist2.DateM'”
当我在Access中运行查询时,它按预期工作。我错过了一些明显的东西吗有什么关于我从Excel调用SQL语句可能会导致这种情况的方法吗?
SELECT Hist.DateM, Sum(Hist.Oil) AS SumOfOil, Sum(Hist.Gas) AS SumOfGas, Sum(Hist.GasSold) AS SumOfGasSold, Sum(Hist.Water) AS SumOfWater, Sum(Hist.InjWater) AS SumOfInjWater, Sum(Hist.DspdWater) AS SumOfDspdWater, OilCount.OilCount AS PrdCount, InjCount.InjCount
FROM ((MoPrdData AS Hist INNER JOIN WellMaster AS Loc ON Hist.WellID = Loc.LocationID)
LEFT OUTER JOIN (
SELECT Hist2.DateM, Count(Hist2.Oil) AS OilCount
FROM MoPrdData Hist2 INNER JOIN WellMaster Loc2 ON Hist2.WellID = Loc2.LocationId
WHERE Loc2.LeaseID = "26.00726" AND Hist2.Oil <> 0
GROUP BY Hist2.DateM ) AS OilCount ON Hist.DateM = OilCount.DateM)
LEFT OUTER JOIN (
SELECT Hist3.DateM, Count(Hist3.InjWater) AS InjCount
FROM MoPrdData Hist3 INNER JOIN WellMaster Loc3 ON Hist3.WellID = Loc3.LocationId
WHERE Loc3.LeaseID = "26.00726" AND Hist3.InjWater <> 0
GROUP BY Hist3.DateM ) AS InjCount ON Hist.DateM = InjCount.DateM
WHERE Loc.LeaseID="26.00726"
GROUP BY Hist.DateM, OilCount.OilCount, InjCount.InjCount;
编辑:可能应该包括我正在使用的VBA,因为这可能是问题。
Sub GrabLeaseData()
'Clear the data
Sheets("PDE").Range("D2:XFD1048576").Clear
'Create links to data sources
Dim PWO As DAO.Database
Set PWO = Workspaces(0).OpenDatabase("PrdAcct", dbDriverNoPrompt, True, "ODBC;DSN=PrdAcct;DATABASE=PrdAcct")
Dim strSQL As String
Dim LeaseID As String
LeaseID = "26.00726" 'TODO: Parameterize this
'Grab data
Dim Data As DAO.Recordset
strSQL = "SELECT Hist.DateM, Sum(Hist.Oil) AS SumOfOil, Sum(Hist.Gas) AS SumOfGas, Sum(Hist.GasSold) AS SumOfGasSold, Sum(Hist.Water) AS SumOfWater, Sum(Hist.InjWater) AS SumOfInjWater, Sum(Hist.DspdWater) AS SumOfDspdWater, OilCount.OilCount AS PrdCount, InjCount.InjCount" & _
"FROM ((MoPrdData AS Hist INNER JOIN WellMaster AS Loc ON Hist.WellID = Loc.LocationID) " & _
"LEFT OUTER JOIN ( " & _
"SELECT Hist2.DateM, Count(Hist2.Oil) AS OilCount " & _
"FROM MoPrdData Hist2 INNER JOIN WellMaster Loc2 ON Hist2.WellID = Loc2.LocationId " & _
"WHERE Loc2.LeaseID = """ & LeaseID & """ AND Hist2.Oil <> 0 " & _
"GROUP BY Hist2.DateM ) AS OilCount ON Hist.DateM = OilCount.DateM) " & _
"LEFT OUTER JOIN ( " & _
"SELECT Hist3.DateM, Count(Hist3.InjWater) AS InjCount " & _
"FROM MoPrdData Hist3 INNER JOIN WellMaster Loc3 ON Hist3.WellID = Loc3.LocationId " & _
"WHERE Loc3.LeaseID = """ & LeaseID & """ AND Hist3.InjWater <> 0 " & _
"GROUP BY Hist3.DateM ) AS InjCount ON Hist.DateM = InjCount.DateM " & _
"WHERE Loc.LeaseID= """ & LeaseID & """ " & _
"GROUP BY Hist.DateM, OilCount.OilCount, InjCount.InjCount; "
Debug.Print strSQL
Set Data = PWO.OpenRecordset(strSQL)
'Process results
End Sub
答案 0 :(得分:0)
这是一个简单的调试问题。
将查询剪切并粘贴到SSMS中。为@LeaseId创建一个变量。声明并设置变量 一旦在SSMS中使用它,请将其修改为在VBA中工作。
我现在会告诉你,我确实看到了一些我不喜欢的括号。
此外,您可以将其转换为存储过程,然后将LeaseId作为参数。在VBA端编写较少的SQL编码,它将被编译。
<强>的问题:强>
1 - 您正在通过OilCount.OCVar对其进行分组,而不是将其列在顶部。为什么呢?
2 - OilCount.OilCount AS PrdCount应该是SUM(OilCount.OilCount)AS PrdCount,如果我理解你想要做什么的话。
分组时,列必须是group by子句的一部分,或者是sum,min,max等聚合函数。
USER TYPO - 通过分组使用OilCount修复。
试试这个,让我知道你是如何做出来的。
-- Add sample lease number
DECLARE @LeaseID VARCHAR(12) = '12345678';
-- Try query
SELECT
Hist.DateM,
OilCount.OilCount AS PrdCount,
InjCount.InjCount,
Sum(Hist.Oil) AS SumOfOil,
Sum(Hist.Gas) AS SumOfGas,
Sum(Hist.GasSold) AS SumOfGasSold,
Sum(Hist.Water) AS SumOfWater,
Sum(Hist.InjWater) AS SumOfInjWater,
Sum(Hist.DspdWater) AS SumOfDspdWater
FROM
MoPrdData AS Hist
INNER JOIN
WellMaster AS Loc ON Hist.WellID = Loc.LocationID
LEFT OUTER JOIN
( SELECT Hist2.DateM, Count(Hist2.Oil) AS OilCount
FROM MoPrdData Hist2
INNER JOIN WellMaster Loc2 ON Hist2.WellID = Loc2.LocationId
WHERE Loc2.LeaseID = @LeaseID AND Hist2.Oil <> 0
GROUP BY Hist2.DateM
) AS OilCount ON Hist.DateM = OilCount.DateM
LEFT OUTER JOIN
( SELECT Hist3.DateM, Count(Hist3.InjWater) AS InjCount
FROM MoPrdData Hist3
INNER JOIN WellMaster Loc3 ON Hist3.WellID = Loc3.LocationId
WHERE Loc3.LeaseID = @LeaseID AND Hist3.InjWater <> 0
GROUP BY Hist3.DateM
) AS InjCount ON Hist.DateM = InjCount.DateM
WHERE
Loc.LeaseID = @LeaseID
GROUP BY
Hist.DateM,
OilCount.OilCount,
InjCount.InjCount