0

I'm trying to construct an SQL statement to return values from the Activity Table & Accounts Table.

I'm working with VBA which connects my Excel workbook to the Access database. At the moment, whenever I run this query it breaks and I can't figure out why as the whole program crashes. I think it has something to do with the sub queries for Outbound Calls, Credit Reviews & Correspondence Issued:

Select m.CaseManager, 
       sum(m.Lead) As 'Total Leads', 
       Count(m.AccountNumber) As 'Total Accounts', 
       Count(a.AccountNumber) As 'Total Activity', 
       (select count(accountnumber) 
        from ActivityTable 
        where Description like 'Outgoing Call%';) As 'Outbound Calls', 
       (select count(accountnumber) 
        from ActivityTable 
        where Description like 'Correspondence Sent%';) As 'Correspondence Issued', 
       (select count(accountnumber) 
        from ActivityTable 
        where Description like 'Credit%';) As 'Credit Reviews'
From AccountTable m, 
     ActivityTable a 
Group By m.CaseManager;
4
  • 1
    I can say that semicolons in the middle of a query is not valid. I am voting to close as typo. Commented Mar 31, 2021 at 10:49
  • Thanks but issue still persists after I remove the semi-colons. Commented Mar 31, 2021 at 10:53
  • You must specify tables relation condition. And use conditional aggregation instead of subqueries. Commented Mar 31, 2021 at 11:05
  • Tried using conditional aggregation using Case, When but this is not supported by MS Access. Have you any suggestions on how to count description based on the conditions above? Commented Mar 31, 2021 at 11:33

2 Answers 2

0

Edit: I looked at the tag and assumed you were using that database. You're not, you're using Access. This answer is mostly worthless. Sorry

You're close. You have a few related problems in your query. Gordon already flagged the extra semicolons.

First, instead of subqueries like

   (select count(accountnumber) 
    from ActivityTable 
    where Description like 'Outgoing Call%') 

use conditional aggregation like this

   SUM( IF(Description LIKE 'Outgoing Call%'), 1, 0)

It sums up a bunch of 1 values for rows with the matching criterion.

Second, you used FROM AccountTable m, ActivityTable a to identify the tables in your query. For one thing, that tells the database to use every possible pairwise combination of rows. That's far too many rows and will give wrong results. For another, it's called the "comma join" syntax and has been obsolete, seriously, since 1992.

You want to JOIN your two tables together so you get the Activity rows associated with each Account row. I guess each separate row in ActivityTable relates back to exactly one row in AccountTable. But that could be wrong. I think you want this.

FROM AccountTable m
JOIN ActivityTable a ON m.AccountNumber=a.AccountNumber

Third, I believe you want to do COUNT(DISTINCT m.AccountNumber) to get each case manager's number of accounts. And you want COUNT(*) for the activity count.

Putting it all together, I guess it looks like this, in the MySQL dialect of SQL. But I cannot test this because I don't have your setup. The VBA in your spreadsheet should pass it through to MySQL unchanged, but it might not. Microsoft's testers don't really care whether VBA works properly with non-Microsoft products.

SELECT m.CaseManager,
       SUM(m.Lead) AS 'Total Leads',
       COUNT(DISTINCT m.AccountNumber) AS 'Total Accounts',
       COUNT(*) AS 'Total Activity',
       SUM(IF(Description LIKE 'Outgoing Call%'), 1, 0) AS 'Outbound Calls',
       SUM(IF(Description LIKE 'Correspondence Sent%'), 1, 0) AS 'Correspondence Issued',
       SUM(IF(Description LIKE 'Credit%'), 1, 0) AS 'Credit Reviews'
  FROM AccountTable m
  JOIN ActivityTable a ON m.AccountNumber=a.AccountNumber
 GROUP BY m.CaseManager;

There's a significant amount of business logic in this aggregating query. SQL offers you a lot to learn. Don't give up!

Sign up to request clarification or add additional context in comments.

3 Comments

This is super - really appreciate the effort. I'm currently seeing an error: Missing operator in query expression 'COUNT(DISTINCT m.AccountNumber)'. Any idea why this might be?
Ugh. That's an excel/VBA message. Maybe COUNT(DISTINCT...) doesn't work there. What happens when you try the query leaving out that line?
I'm now seeing a new error that SELECT statement includes a word that is misspelled, missing or the punctuation is incorrect... double checks and all heading match the query.
0

Your trying to do a subquery inside a totals query. That is too complicated for me to figure out: instead consider adding a calculated variable to determine if a record is also a outgoing call and then letting the totals query sum that up to: assuming your access tables are structured like:

enter image description here

Outgoing Calls: IIf([ActivityTable].[Description] Like "Outgoing Call*",1,0)
Correspondence Sent: IIf([ActivityTable].[Description] Like "Correspondence Sent*",1,0)
Credit Reviews: IIf([ActivityTable].[Description] Like "Credit*",1,0)

here is the generated sql you can just paste into the sql pane if you have the corresponding tables

SELECT AccountTable.CaseManager, Sum(AccountTable.Lead) AS [total leads], Count(AccountTable.AccountNumber) AS [Total Accounts], Count(ActivityTable.AccountNumber) AS [Total Activity], Sum(IIf([ActivityTable].[Description] Like "Outgoing Call*",1,0)) AS [Outgoing Calls], Sum(IIf([ActivityTable].[Description] Like "Correspondence Sent*",1,0)) AS [Correspondence Sent], Sum(IIf([ActivityTable].[Description] Like "Credit*",1,0)) AS [Credit Reviews]
FROM AccountTable INNER JOIN ActivityTable ON AccountTable.AccountNumber = ActivityTable.AccountNumber
GROUP BY AccountTable.CaseManager;

note: for this example to work AccountTable.Accountnumber has to be set as unique or access will not allow the relationship as AccountNumber is not set up as the primary key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.