473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

query is too complex

Hi,

I am using a series of queries containing IIF statements and calculated fields to perform a calcualtion between two dates with a further time period subtracted. It works except I wanted to sort (ascending) the resulting caluclated field (RefToTreat in the last query); I got a 'query is too complex' error. Any ideas how I can avoid/work around this?

The SQL of the queries is as follows:

qryEffectiveStartDate_Assessment - determines the latest of six dates in tblContactDetails and tblAssessmentNonAttendance.

Expand|Select|Wrap|Line Numbers
  1. SELECT tblContactDetails.ContactNumber, tblContactDetails.HospitalNumber, tblContactDetails.[ReferralDate/1stContact], IIf([Non-AttendanceCategory1]=2,[Non-AttendanceDate1],IIf([Non-AttendanceCategory1]=3,[Non-AttendanceDate1],[ReferralDate/1stContact])) AS StartDate1, IIf([Non-AttendanceCategory2]=2,[Non-AttendanceDate2],IIf([Non-AttendanceCategory2]=3,[Non-AttendanceDate2],[ReferralDate/1stContact])) AS StartDate2, IIf([Non-AttendanceCategory3]=2,[Non-AttendanceDate3],IIf([Non-AttendanceCategory3]=3,[Non-AttendanceDate3],[ReferralDate/1stContact])) AS StartDate3, IIf([Non-AttendanceCategory4]=2,[Non-AttendanceDate4],IIf([Non-AttendanceCategory4]=3,[Non-AttendanceDate4],[ReferralDate/1stContact])) AS StartDate4, IIf([Non-AttendanceCategory5]=2,[Non-AttendanceDate5],IIf([Non-AttendanceCategory5]=3,[Non-AttendanceDate5],[ReferralDate/1stContact])) AS StartDate5, IIf([StartDate5]>[StartDate4],[StartDate5],IIf([StartDate4]>[StartDate3],[StartDate4],IIf([StartDate3]>[StartDate2],[StartDate3],IIf([StartDate2]>[StartDate1],[StartDate2],[StartDate1])))) AS EffectiveStartDate_Assessment
  2. FROM tblContactDetails LEFT JOIN tblAssessmentNonAttendance ON tblContactDetails.ContactNumber = tblAssessmentNonAttendance.ContactNumber
  3. WHERE (((tblContactDetails.[ReferralDate/1stContact])>#3/31/2011#));
  4.  

qryEffectiveStartDate_FirstTreatment - determines the latest of six dates in tblContactDetails and tblTreatmentNonAttendance.

Expand|Select|Wrap|Line Numbers
  1. SELECT tblContactDetails.HospitalNumber, tblContactDetails.ContactNumber, tblContactDetails.[ReferralDate/1stContact], IIf([TreatmentNon-AttendanceCategory1]=2,[TreatmentNon-AttendanceDate1],IIf([TreatmentNon-AttendanceCategory1]=3,[TreatmentNon-AttendanceDate1],[ReferralDate/1stContact])) AS StartDate1_FirstTreat, IIf([TreatmentNon-AttendanceCategory2]=2,[TreatmentNon-AttendanceDate2],IIf([TreatmentNon-AttendanceCategory2]=3,[TreatmentNon-AttendanceDate2],[ReferralDate/1stContact])) AS StartDate2_FirstTreat, IIf([TreatmentNon-AttendanceCategory3]=2,[TreatmentNon-AttendanceDate3],IIf([TreatmentNon-AttendanceCategory3]=3,[TreatmentNon-AttendanceDate3],[ReferralDate/1stContact])) AS StartDate3_FirstTreat, IIf([TreatmentNon-AttendanceCategory4]=2,[TreatmentNon-AttendanceDate4],IIf([TreatmentNon-AttendanceCategory4]=3,[TreatmentNon-AttendanceDate4],[ReferralDate/1stContact])) AS StartDate4_FirstTreat, IIf([TreatmentNon-AttendanceCategory17]=2,[TreatmentNon-AttendanceDate17],IIf([TreatmentNon-AttendanceCategory17]=3,[TreatmentNon-AttendanceDate17],[ReferralDate/1stContact])) AS StartDate5_FirstTreat, IIf([StartDate5_FirstTreat]>[StartDate4_FirstTreat],[StartDate5_FirstTreat],IIf([StartDate4_FirstTreat]>[StartDate3_FirstTreat],[StartDate4_FirstTreat],IIf([StartDate3_FirstTreat]>[StartDate2_FirstTreat],[StartDate3_FirstTreat],IIf([StartDate2_FirstTreat]>[StartDate1_FirstTreat],[StartDate2_FirstTreat],[StartDate1_FirstTreat])))) AS EffectiveStartDate_FirstTreat
  2. FROM tblContactDetails LEFT JOIN tblTreatmentNonAttendance ON tblContactDetails.ContactNumber = tblTreatmentNonAttendance.ContactNumber
  3. WHERE (((tblContactDetails.[ReferralDate/1stContact])>#3/31/2011#));

qryEffectiveStartDate - determines the latest of the two dates returned by the two previous queries. This is [EffectiveStartDate], the start point in the last query (below).

Expand|Select|Wrap|Line Numbers
  1. SELECT tblContactDetails.HospitalNumber, tblContactDetails.ContactNumber, tblContactDetails.[ReferralDate/1stContact], qryEffectiveStartDate_Assessment.EffectiveStartDate_Assessment, qryEffectiveStartDate_FirstTreatment.EffectiveStartDate_FirstTreat, IIf([EffectiveStartDate_FirstTreat]>[EffectiveStartDate_Assessment],[EffectiveStartDate_FirstTreat],[EffectiveStartDate_Assessment]) AS EffectiveStartDate
  2. FROM (qryEffectiveStartDate_Assessment RIGHT JOIN tblContactDetails ON qryEffectiveStartDate_Assessment.ContactNumber = tblContactDetails.ContactNumber) LEFT JOIN qryEffectiveStartDate_FirstTreatment ON tblContactDetails.ContactNumber = qryEffectiveStartDate_FirstTreatment.ContactNumber
  3. WHERE (((tblContactDetails.[ReferralDate/1stContact])>#3/31/2011#));

qryPeriodsOfUnavailability_FirstTreat - calculates the periods a person is not available and totals these ([TotalUnavailability]) to be subtracted in the last query below.

Expand|Select|Wrap|Line Numbers
  1. SELECT tblContactDetails.HospitalNumber, tblContactDetails.ContactNumber, tblContactDetails.[ReferralDate/1stContact], DateDiff('d',[UnavailabilityStartDate1],[UnavailabilityEndDate1]) AS Period1, DateDiff('d',[UnavailabilityStartDate2],[UnavailabilityEndDate2]) AS Period2, DateDiff('d',[UnavailabilityStartDate3],[UnavailabilityEndDate3]) AS Period3, DateDiff('d',[UnavailabilityStartDate4],[UnavailabilityEndDate4]) AS Period4, DateDiff('d',[UnavailabilityStartDate5],[UnavailabilityEndDate5]) AS Period5, IIf([UnavailabilityStartDate1]>=[EffectiveStartDate],[Period1],0) AS ExcludePeriod1a, IIf([UnavailabilityStartDate1]<=[DateTreatmentStarted1],[Period1],0) AS ExcludePeriod1b, IIf([ExcludePeriod1a]>[ExcludePeriod1b],[ExcludePeriod1a],[ExcludePeriod1b]) AS ExcludePeriod1, IIf([UnavailabilityStartDate2]>=[EffectiveStartDate],[Period2],0) AS ExcludePeriod2a, IIf([UnavailabilityStartDate2]<=[DateTreatmentStarted1],[Period2],0) AS ExcludePeriod2b, IIf([ExcludePeriod2a]>[ExcludePeriod2b],[ExcludePeriod2a],[ExcludePeriod2b]) AS ExcludePeriod2, IIf([UnavailabilityStartDate3]>=[EffectiveStartDate],[Period3],0) AS ExcludePeriod3a, IIf([UnavailabilityStartDate3]<=[DateTreatmentStarted1],[Period3],0) AS ExcludePeriod3b, IIf([ExcludePeriod3a]>[ExcludePeriod3b],[ExcludePeriod3a],[ExcludePeriod3b]) AS ExcludePeriod3, IIf([UnavailabilityStartDate4]>=[EffectiveStartDate],[Period4],0) AS ExcludePeriod4a, IIf([UnavailabilityStartDate4]<=[DateTreatmentStarted1],[Period4],0) AS ExcludePeriod4b, IIf([ExcludePeriod4a]>[ExcludePeriod4b],[ExcludePeriod4a],[ExcludePeriod4b]) AS ExcludePeriod4, IIf([UnavailabilityStartDate5]>=[EffectiveStartDate],[Period5],0) AS ExcludePeriod5a, IIf([UnavailabilityStartDate5]<=[DateTreatmentStarted1],[Period5],0) AS ExcludePeriod5b, IIf([ExcludePeriod5a]>[ExcludePeriod5b],[ExcludePeriod5a],[ExcludePeriod5b]) AS ExcludePeriod5, [ExcludePeriod1]+[ExcludePeriod2]+[ExcludePeriod3]+[ExcludePeriod4]+[ExcludePeriod5] AS TotalUnavailability
  2. FROM (qryEffectiveStartDate RIGHT JOIN tblContactDetails ON qryEffectiveStartDate.ContactNumber = tblContactDetails.ContactNumber) LEFT JOIN tblUnavailability ON tblContactDetails.ContactNumber = tblUnavailability.ContactNumber
  3. WHERE (((tblContactDetails.[ReferralDate/1stContact])>#3/31/2011#));

qryReferraltoFirstTreatment - calculates the time between [EffectiveStartDate] and [DateTreatmentStarted1] minus [TotalUnavailability] - this is field I wish to sort.

Expand|Select|Wrap|Line Numbers
  1. SELECT tblPatientDetails.PatientForename, tblPatientDetails.PatientSurname, tblPatientDetails.HospitalNumber, tblPatientDetails.HospitalNumber2, tblContactDetails.[ReferralDate/1stContact], qryEffectiveStartDate.EffectiveStartDate, tblContactDetails.DateTreatmentStarted1, qryPeriodsOfUnavailability_FirstTreat.TotalUnavailability, ([DateTreatmentStarted1]-[EffectiveStartDate])-[TotalUnavailability] AS RefToTreat
  2. FROM tblPatientDetails INNER JOIN ((qryEffectiveStartDate RIGHT JOIN tblContactDetails ON qryEffectiveStartDate.ContactNumber = tblContactDetails.ContactNumber) LEFT JOIN qryPeriodsOfUnavailability_FirstTreat ON tblContactDetails.ContactNumber = qryPeriodsOfUnavailability_FirstTreat.ContactNumber) ON tblPatientDetails.HospitalNumber = tblContactDetails.HospitalNumber
  3. WHERE (((tblContactDetails.[ReferralDate/1stContact])>#3/31/2011#));
thanks
L
Jun 15 '11 #1
8 2211
beacon
579 512MB
Hi L,

I'm going to throw this out there for you to check on first (before I look at the queries in depth). Are all of the table fields that you are working with as dates set as date/time fields on the underlying table?

I seem to remember running into a similar error a long time ago, and that I was able to fix it by converting one of the fields from text to date (using the CDate() function) in the query (instead of changing the data type from text to date/time in the underlying table).

Try this on for size and see if that's the issue.

Hope this helps,
beacon
Jun 15 '11 #2
Hi Beacon,

I wish it were that simple, but alas all the date fields from tables are set as date/time fields (short date).

L
Jun 15 '11 #3
beacon
579 512MB
This is just a shot in the dark because nothing is jumping out at me on your SQL, but are your queries setup so that you could perform the WHERE clause on just the last query in the group? I'm curious if maybe that is having an effect on how the data is processing.

I performed a search on your error message and returned the following link from the Microsoft Knowledge Base: http://support.microsoft.com/kb/103429

Two things that jump out from the article that you may want to fix:
  1. Move complex expressions to a user-defined function that does all the evaluating
  2. Reduce expressions in underlying queries
Jun 15 '11 #4
NeoPa
32,556 Expert Mod 16PB
Generally I would tidy up your SQL. There's many times an appropriate amount of SQL to ask someone to go through for you. What I did see was that there is scope for using the Choose() function in place of some of the IIf()s, but I'm sure there is scope for more. Unless you're doing rocket science I can't imagine you need anything as complex (clumsy) as you have there. I may be wrong, but I've never seen anything as complex as that that eventually proved necessary.

The real lesson here is not ever to let your SQL (or any other code for that matter) get into such a state. If you're tidy when you build it up, it need never get to this state.

By the way, it is more likely that people will look at your code for you if you format it for display on the page so that it's readable. SQL doesn't care where it finds its commands except that it recognizes white space. Here's an example to show that even very large and clumsy SQL can be legible at least :

Expand|Select|Wrap|Line Numbers
  1. SELECT tPD.PatientForename
  2.      , tPD.PatientSurname
  3.      , tPD.HospitalNumber
  4.      , tPD.HospitalNumber2
  5.      , tCD.[ReferralDate/1stContact]
  6.      , qESD.EffectiveStartDate
  7.      , tCD.DateTreatmentStarted1
  8.      , qPUF.TotalUnavailability
  9.      , [DateTreatmentStarted1]-
  10.        [EffectiveStartDate]-
  11.        [TotalUnavailability] AS [RefToTreat]
  12.  
  13. FROM   [tblPatientDetails] AS tPD
  14.        INNER JOIN
  15.      (([qryEffectiveStartDate] AS qESD
  16.        RIGHT JOIN
  17.        [tCD.] AS tCD
  18.   ON   qESD.ContactNumber = tCD.ContactNumber)
  19.        LEFT JOIN
  20.        [qryPeriodsOfUnavailability_FirstTreat] AS qPUF
  21.   ON   tCD.ContactNumber = qPUF.ContactNumber)
  22.   ON   tPD.HospitalNumber = tCD.HospitalNumber
  23.  
  24. WHERE  (tCD.[ReferralDate/1stContact]>#3/31/2011#)
Jun 15 '11 #5
Stewart Ross
2,545 Expert Mod 2GB
To add to what my very experienced colleague NeoPa has said, when SQL expressions start to look complex or untidy it points to problems in either the way the data is defined or the way the SQL is being used to process that data. If it looks complex it is often an indication that something is missing elsewhere in the approach to the problem - something I think relating in your case to defining how patient appointment misses are calculated and recorded.

In the examples you provide, the nested IIFs are quite difficult to follow, and I would echo what Beacon has mentioned (from the MS knowledgebase article) about replacing these with custom functions to do the processing for you. The query optimisers in the Access database engine cannot optimise IIF statements, and I would bet that the 'query too complex' error results from the problems the query engine faces in dealing with six levels of SQL statements, five of which have LEFT or RIGHT joins, each of which contains multiple (sometimes nested) IIFs.

I do not know enough about your application to advise how to optimise it, but it does feel as if the rule set you are applying through your IIFs indicates either that there may be more tables needed than you currently have (if some of the work can be done by joining to a lookup table based on current categories), or that custom functions may be needed (to implement specific rules that can't be encoded in a lookup table, such as which date to apply given the number of missed appointments etc).

-Stewart
Jun 16 '11 #6
Hi all,

Thanks for the responses. For ease and speed I have decided to make the final query a make table one and do further querying from there - clumsy I'm sure but it works for now.

NeoPa - I wrote the queries in design view and simply copied from the SQL window to paste into my question. I'm not very familiar with using SQL but will endeavour to simplify this code and replace the above solution with a neater one. Thanks for the tip re format also.

Beacon, Stewart - again I'm not familiar with user-defined functions but have started to do a little research. It seems these are only available in ADP not databases - am I missing something?

thanks again
L
Jun 20 '11 #7
Stewart Ross
2,545 Expert Mod 2GB
I'm puzzled about the source of your info re ADP and functions, Lucie, as you can define functions within a code module in any form of Access database, just as you can in Excel and the other Office applications.

-Stewart
Jun 20 '11 #8
NeoPa
32,556 Expert Mod 16PB
If, while designing an Access object (like a report, or even one of the many sections or controls on one), you look at the properties window, you will find a number of Event properties (On Click; After Update; On Enter; etc). Double-clicking the names of any of these properties will set the value to "[Event Procedure]". If you then click on the ellipsis button immediately to the right a new window will open allowing access to the code of your project. It will also, in this case, have created the stub of an Event Procedure for you. In here you can create and modify (and even monitor the execution of) your project's code (See Debugging in VBA for more on this).

I would say most MDB files, certainly of those done at a professional level, will have some code associated. If you need help with any of this please feel free to post your specific question (or questions) in the forum.
Jun 20 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris | last post by:
Hello all- Fairly new to SQL and I need to issue a pretty complex query (complex being a relative term here :) ). To dumb down my example for display purposes, I have two tables in my schema...
3
by: Brian Oster | last post by:
After applying security patch MS03-031 (Sql server ver 8.00.818) a query that used to execute in under 2 seconds, now takes over 8 Minutes to complete. Any ideas on what the heck might be going...
4
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
3
by: Jonathan | last post by:
Hi all! For a match schedule I would like to find all possible combinations of teams playing home and away (without teams playing to themselves of course). I now the simple version works...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
10
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for potential conflicts of interest based either on Social...
1
by: arun | last post by:
Query is too complex -------------------------------------------------------------------------------- Hi, I was trying to solve this problem since last two days but couldn't find any solution. ...
1
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
21
wordbrew
by: wordbrew | last post by:
I thought I was done with my database, everything was working great, but after going into the query that my subform is based on and simply changing a field from ascending to descending, then closing...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.