Solved

Simplifying my select statement

Posted on 2008-10-24
9
293 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

757 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

23 Experts available now in Live!

Get 1:1 Help Now