SQL calculating two different things from same data

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

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:
OK, I tried doing a real simple example in Access, where I had one query for ID, and three for the months, and here is the SQL code it came up with:
Code:
SELECT [qry1-IDs].ID, Nz([qry2-Month1]![SumOfAmount],0)+0 AS Month1, Nz([qry3-Month2]![SumOfAmount],0)+0 AS Month2, Nz([qry4-Month3]![SumOfAmount],0)+0 AS Month3
FROM [B][COLOR=#ff0000](([/COLOR][/B][qry1-IDs] 
LEFT JOIN [qry2-Month1] 
ON [qry1-IDs].ID = [qry2-Month1].ID[B][COLOR=#ff0000])[/COLOR][/B] 
LEFT JOIN [qry3-Month2] 
ON [qry1-IDs].ID = [qry3-Month2].ID[B][COLOR=#ff0000])[/COLOR][/B] 
LEFT JOIN [qry4-Month3] 
ON [qry1-IDs].ID = [qry4-Month3].ID;
Pay special attention to the parentheses I have highlighted in red. So maybe you need to structure/nest the code like that.
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
YES

Ahem I mean, yes, thanks again. I was sure it had something to do with parentheses and by confirming this I was able to get it after a few more attempts. Another useful trick added to the Excel toolkit :D
 
Upvote 0
You are welcome!

Glad we figured it out and got it working!
:)
 
Upvote 0

Forum statistics

Threads
1,214,566
Messages
6,120,257
Members
448,952
Latest member
kjurney

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top