baitmaster
Well-known Member
- Joined
- Mar 12, 2009
- Messages
- 2,042
Hi, I'm trying to use SQL within VBA to calculate several related things at once, but I'm doing something wrong
I have 4 columns I'm interested in: ID, Description, Value and Date (see code for exact labels). I want to return a dataset that contains
- unique ID
- unique Description
- sum of all values up to end of this month
- sum of all values up to end of last month
I think I need to create two sets of calculations and then join the results
My current attempt has been to do this but use UNION on the two sets of results, which I can see is wrong. Pretty sure I need to be Joining the results but just getting syntax errors. I've split into 2 separate subqueries for clarity
The results I'm getting are in 3 columns so clearly the values are being merged, which is not what I want - I'm trying to keep two separate columns
The final SQL string I'm getting is
SELECT *
FROM (
SELECT [data$].[Proj Desc], [data$].[Proj ID], SUM([data$].[Monetary AMT ]) AS CurrMonth
FROM [data$]
WHERE [data$].[Account ] = 120009 And [data$].[Journal Date ] <= 43585
GROUP BY [data$].[Proj ID], [data$].[Proj Desc]
UNION
SELECT [data$].[Proj Desc], [data$].[Proj ID], SUM([data$].[Monetary AMT ]) AS PriorMonth
FROM [data$]
WHERE [data$].[Account ] = 120009 And [data$].[Journal Date ] <= 43555
GROUP BY [data$].[Proj ID], [data$].[Proj Desc]
);
I have 4 columns I'm interested in: ID, Description, Value and Date (see code for exact labels). I want to return a dataset that contains
- unique ID
- unique Description
- sum of all values up to end of this month
- sum of all values up to end of last month
I think I need to create two sets of calculations and then join the results
My current attempt has been to do this but use UNION on the two sets of results, which I can see is wrong. Pretty sure I need to be Joining the results but just getting syntax errors. I've split into 2 separate subqueries for clarity
Code:
Function getRsReport1Sum(lAcct As Long, lDate As Long, lDatePrior As Long) As ADODB.Recordset
' create SQL string
Dim strTbl1 As String, strTbl2 As String, strSql As String
strTbl1 = "SELECT " & _
"[data$].[Proj Desc], [data$].[Proj ID], SUM([data$].[Monetary AMT ]) AS CurrMonth " & _
"FROM [data$] " & _
"WHERE [data$].[Account ] = " & lAcct & " " & _
"AND [data$].[Journal Date ] <= " & lDate & " " & _
"GROUP BY [data$].[Proj ID], [data$].[Proj Desc] "
strTbl2 = "SELECT " & _
"[data$].[Proj Desc], [data$].[Proj ID], SUM([data$].[Monetary AMT ]) AS PriorMonth " & _
"FROM [data$] " & _
"WHERE [data$].[Account ] = " & lAcct & " " & _
"AND [data$].[Journal Date ] <= " & lDatePrior & " " & _
"GROUP BY [data$].[Proj ID], [data$].[Proj Desc] "
strSql = "SELECT * FROM (" & _
strTbl1 & _
"UNION " & _
strTbl2 & _
");"
The results I'm getting are in 3 columns so clearly the values are being merged, which is not what I want - I'm trying to keep two separate columns
The final SQL string I'm getting is
SELECT *
FROM (
SELECT [data$].[Proj Desc], [data$].[Proj ID], SUM([data$].[Monetary AMT ]) AS CurrMonth
FROM [data$]
WHERE [data$].[Account ] = 120009 And [data$].[Journal Date ] <= 43585
GROUP BY [data$].[Proj ID], [data$].[Proj Desc]
UNION
SELECT [data$].[Proj Desc], [data$].[Proj ID], SUM([data$].[Monetary AMT ]) AS PriorMonth
FROM [data$]
WHERE [data$].[Account ] = 120009 And [data$].[Journal Date ] <= 43555
GROUP BY [data$].[Proj ID], [data$].[Proj Desc]
);
Last edited: