Large Query Aggregate function error (1 Viewer)

twiggy

New member
Local time
Today, 19:36
Joined
Sep 15, 2006
Messages
2
Hi guys,

I am completely at a lose here. I have a LARGE sql query that was written before my time and how have the task of customizing it a little. It will run perfectly in Advisor Office (the program it was written for which uses access as the back end db). But if i try and create a query in access directly i get the following error:

You tried to execute a query that does not include the specified expression 'IIF (CommnPremiums.Type IN (1, 2), IIF (CommnEntries.Type IN (1, 4), 'Non Indemnity', IIF (CommnEntries.Type IN (2, 6), 'Indemnity', IIF (CommnEntries.Type = 3, 'Renewal', IIF (CommnEntries.Type = 5, 'Trail', ' ')))),
IIF (CommnPremiums.Type = 3, 'Initial', IIF (CommnPremiums.Type =' as part of an aggregate function

the full IIF statement that is causing it to fail is:

PHP:
IIF (CommnPremiums.Type IN (1, 2), IIF (CommnEntries.Type IN (1, 4),  'Non Indemnity', IIF (CommnEntries.Type IN (2, 6), 'Indemnity', IIF (CommnEntries.Type = 3, 'Renewal', IIF (CommnEntries.Type = 5, 'Trail', ' ')))), 
IIF (CommnPremiums.Type = 3, 'Initial', IIF (CommnPremiums.Type = 4, 'Renewal', IIF (CommnPremiums.Type = 5, 'Trail', IIF (CommnPremiums.Type = 6, 'Protected Rights', IIF (CommnPremiums.Type = 7, 'Transfer', ' ')))))) AS CommissionType

The WHOLE SQL query spans over 4 pages of A4 so i will only put it at the bottom (hoping its not needed to see the error. There are a few unions so i will put the first one in here to show what it is doing.

PHP:
SELECT IIF (Policies.Owner = 0, clients.partnersurname + ", " + clients.partnerforenames, clients.surname + ", " + clients.forenames) AS Name,
Policies.PolicyNumber,
Schemes.SchemeName,
Managers.FullName AS Manager,
CommnPremiums.CommnPremiumDate,
IIF (CommnPremiums.Type = 1, 'Initial Premium', IIF (CommnPremiums.Type = 2, 'Increment', IIF (CommnPremiums.Type =  3, 'Single Premium', IIF (CommnPremiums.Type = 4, 'Annual Renewal', IIF (CommnPremiums.Type = 5, 'Value Based', IIF (CommnPremiums.Type = 6, 'Protected Rights', IIF (CommnPremiums.Type = 7, 'Transfer', ' '))))))) + 
IIF (CommnPremiums.Type IN (1, 2), IIF (CommnEntries.Type = 1,  ' - Non Indemnity', IIF (CommnEntries.Type = 2, ' - Indemnity', IIF (CommnEntries.Type = 3, ' - Renewal', IIF (CommnEntries.Type = 4, ' - Level', IIF (CommnEntries.Type = 5, ' - Fund Based Trail', IIF (CommnEntries.Type = 6, ' - Level Indemnity', ' ')))))), ' ') AS PremiumType,
CommnEntries.DueFrom,
CommnEntries.DueTo,
SUM (Matches.Amount) AS TotalMatched, 
CommissionCredits.CreditPC AS Credit, 
Consultants.FullName AS Consultant,
ROUND ((CommissionCredits.CreditPC * SUM (Matches.Amount) / 100), 2) AS CommissionMatched,
Cheques.Amount AS ChequeAmount,
Cheques.ChequeDate,
IIF (CommnPremiums.Type IN (1, 2), IIF (CommnEntries.Type IN (1, 4),  'Non Indemnity', IIF (CommnEntries.Type IN (2, 6), 'Indemnity', IIF (CommnEntries.Type = 3, 'Renewal', IIF (CommnEntries.Type = 5, 'Trail', ' ')))), 
IIF (CommnPremiums.Type = 3, 'Initial', IIF (CommnPremiums.Type = 4, 'Renewal', IIF (CommnPremiums.Type = 5, 'Trail', IIF (CommnPremiums.Type = 6, 'Protected Rights', IIF (CommnPremiums.Type = 7, 'Transfer', ' ')))))) AS CommissionType
FROM ((((((((Clients LEFT JOIN Policies ON Clients.ClientRef = Policies.ClientRef) 
LEFT JOIN Schemes ON Policies.SchemeRef = Schemes.SchemeRef)
LEFT JOIN CommnPremiums ON Policies.PolicyRef = CommnPremiums.PolicyRef)
LEFT JOIN CommnEntries ON CommnPremiums.CommnPremRef = CommnEntries.CommnPremRef)
LEFT JOIN Matches ON CommnEntries.CommnEntryRef = Matches.FKeyRef)
LEFT JOIN Cheques ON Matches.ChequeRef = Cheques.ChequeRef)
LEFT JOIN Managers ON Cheques.FKeyRef = Managers.ManagerRef)
LEFT JOIN CommissionCredits ON CommnEntries.CommnEntryRef = CommissionCredits.FKeyRef)
LEFT JOIN Consultants ON CommissionCredits.ConsultantRef = Consultants.ConsultantRef
WHERE Matches.FKeyType = 1
AND Cheques.FKeyType = 1
AND Cheques.ChequeDate BETWEEN #01-Jan-2004# AND #31-Dec-2004#
AND CommissionCredits.FKeyType = 1
AND CommissionCredits.Type = 1
GROUP BY IIF (Policies.Owner = 0, clients.partnersurname + ", " + clients.partnerforenames, clients.surname + ", " + clients.forenames),
Policies.PolicyNumber,
Schemes.SchemeName,
Managers.FullName,
CommnPremiums.CommnPremiumDate,
IIF (CommnPremiums.Type = 1, 'Initial Premium', IIF (CommnPremiums.Type = 2, 'Increment', IIF (CommnPremiums.Type =  3, 'Single Premium', IIF (CommnPremiums.Type = 4, 'Annual Renewal', IIF (CommnPremiums.Type = 5, 'Value Based', IIF (CommnPremiums.Type = 6, 'Protected Rights', IIF (CommnPremiums.Type = 7, 'Transfer', ' '))))))) + 
IIF (CommnPremiums.Type IN (1, 2), IIF (CommnEntries.Type = 1,  ' - Non Indemnity', IIF (CommnEntries.Type = 2, ' - Indemnity', IIF (CommnEntries.Type = 3, ' - Renewal', IIF (CommnEntries.Type = 4, ' - Level', IIF (CommnEntries.Type = 5, ' - Fund Based Trail', IIF (CommnEntries.Type = 6, ' - Level Indemnity', ' ')))))), ' '),
CommnEntries.DueFrom,
CommnEntries.DueTo,
Cheques.Amount,
Cheques.ChequeDate,
IIF (CommnPremiums.Type IN (1, 2), IIF (CommnEntries.Type IN (1, 4),  'Non Indemnity', IIF (CommnEntries.Type IN (2, 6), 'Indemnity', IIF (CommnEntries.Type = 3, 'Renewal', IIF (CommnEntries.Type = 5, 'Trail', ' ')))), 
IIF (CommnPremiums.Type = 3, 'Initial', IIF (CommnPremiums.Type = 4, 'Renewal', IIF (CommnPremiums.Type = 5, 'Trail', IIF (CommnPremiums.Type = 6, 'Protected Rights', IIF (CommnPremiums.Type = 7, ‘Transfer’, ' ')))))), Consultants.FullName, CommissionCredits.CreditPC

If I changed the IIF statement to:

PHP:
'X' as commisiontype

it all works great but i need this information and its driving me mad. I can not see why it doesnt work within Access.

Any and All help would be great.

thx

Twiggy


I have attached the FULL sql query if you want to look at it all.. it hurts my head to read it all ;)
 

Attachments

  • sql.doc
    50 KB · Views: 203
Last edited:

twiggy

New member
Local time
Today, 19:36
Joined
Sep 15, 2006
Messages
2
So it turns out that there were dodgy characters around the place. I retyped it out and it worked.. well almost.. it then asked for a "Transfer" value.. but that was just a text output.. so i deleted all of the transfers and retyped them and it all worked.. how annoying.. spent SOOO long doing this :(
 

Users who are viewing this thread

Top Bottom