Solved

Simplifying my select statement

Posted on 2008-10-24
9
294 Views
Last Modified: 2011-10-19
I have a query defined which needs to calculate averages for differtent parts of a questionnaire. There are now 1000 records in for testing, but the real db contains over 40000 already.
My sql statement now takes like minutes to load for only those 1000 records.

Does anyone have any tips on simplifying the sql statement.

QuestionnaireOffline contains the results from the query with also CodePostal, the table CodePostal contains the province. I need to get overview of the responses per province.
SELECT DISTINCT (SELECT Count(*) FROM QuestionnaireOffline WHERE CodePostal IN (SELECT CP FROM CodesPostal WHERE Province='BRABANT WALLON')) AS nTotal, (SELECT count(*) FROM QuestionnaireOffline WHERE mode = "Papier" AND CodePostal IN (SELECT CP FROM CodesPostal WHERE Province='BRABANT WALLON')) AS nPapier, (SELECT count(*) FROM QuestionnaireOffline WHERE mode = "Internet" AND CodePostal IN (SELECT CP FROM CodesPostal WHERE Province='BRABANT WALLON')) AS nInternet, ...

 

FROM QuestionnaireOffline;

Open in new window

0
Comment
Question by:pmelkebeke
  • 4
  • 4
9 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22794871
SELECT Count(q.CodePostal) AS nTotal, Sum(IIf(q.mode = "Papier", 1, 0) AS nPapier,
    Sum(IIf(q.mode = "Internet", 1, 0) AS nInternet, ...
FROM QuestionnaireOffline q INNER JOIN
    CodesPostal c ON q.CodePostal = c.CP
WHERE c.Province = "BRABANT WALLON"
0
 

Author Comment

by:pmelkebeke
ID: 22795173
I'm missing some kind of a DISTINCT in this picture. The total is way too high.
I have 5 different provinces. If i ran this query on my data then the total amount is way higher than the no of records I have.

I must say, If i sum all the results I get the same number of records as I have when I query (without DISTINCT) in a wrongly way like this:
SELECT * FROM QUESTIONNAIREOFFLINE, Codespostal WHERE Codepostal = CP

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22795523
pmelkebeke,

It would be useful to see some sample data.  EE now allows you to directly upload
files to your question.

Please be advised that once you upload a file, it can be publicly accessed, and that it
may not be possible to fully and permanently delete it.  Therefore, be very careful about
posting proprietary, confidential, or other sensitive information.  If necessary, use "fake"
and/or obfuscated data in your sample.

Regards,

Patrick
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 22795647
This query
SELECT * FROM QUESTIONNAIREOFFLINE, Codespostal WHERE Codepostal = CP
may not work as you expect.  You will get a 'CARTESIAN JOIN'  between the two tables. It should read:
SELECT QUESTIONNAIREOFFLINE.* FROM QUESTIONNAIREOFFLINE JOIN Codespostal on QUESTIONNAIREOFFLINE.CodePostal = Codespostal.CP  
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:pmelkebeke
ID: 22795682
Okay, here it is.
EE-demodata.mdb
0
 

Author Comment

by:pmelkebeke
ID: 22795858
Arthur,

I know this. That's why I posted it. The result I get with the first proposal gives the same results as this cartesian join result...
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 500 total points
ID: 22798607
pmelkebeke,

The problem is that CodesPostal has >1 record for each post code

Regards,

Patrick
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 22798622
This seems to be working against the sample data:

SELECT Count(*) AS nTotal, Sum(IIf(q.Mode = "Papier", 1, 0)) AS nPapier, Sum(IIf(q.Mode = "Internet", 1, 0)) AS nInternet
FROM QuestionnaireOffline q INNER JOIN
    (SELECT c.CP, Max(c.Province) AS Prov FROM CodesPostal c GROUP BY c.CP) cc ON q.CodePostal = cc.CP
WHERE cc.Prov = "BRABANT WALLON";
0
 

Author Closing Comment

by:pmelkebeke
ID: 31509588
Thank you very much for this solution. I saw indeed that there were more results for some postal codes and I fixed it by using a query which filters out the doubles and using that one in the solution query. Thank you very much for the solution.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now