473,511 Members | 14,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

quicker query method?

In PHP, I do the following (pseudo code for clarity)...

Query: SELECT distinct(web_contacts.zip) FROM web_contacts WHERE
web_contacts.zip <> ''
Query: SELECT DISTINCT(contacts.zip) FROM contacts WHERE contacts.zip <> '';

Take all those results put them into an array:
$zips[$zipCode] = 0;

The zipCode is used as the index to avoid duplicates between tables...

Then I retrieve the counts of these zips:

$zips[$zipCode] = Query: SELECT COUNT(web_contacts.zip) FROM
web_contacts WHERE web_contacts.zip = $zipCode
$zips[$zipCode] += Query: SELECT Count(contacts.zip) FROM contacts WHERE
contacts.zip = $zipCode
$zips=arsort($zips)

So this now gives me an array that has all the different zip codes from
2 tables as indicies, and how many total times each of these zips showed
up as the element's value ordered by most hits to least hits. From that,
you can print a "Top X Zips" report by walking the array and printing
each key and value pair until you've reached the X number you want to
report on.

Great, no problems there... However - It's never simple is it? - I now
need to do the same thing in a Microsoft Access database. I tried this:

[zip1]
SELECT distinct(web_contacts.zip) FROM web_contacts WHERE
web_contacts.zip <> '' UNION ALL SELECT DISTINCT(contacts.zip) FROM
contacts WHERE contacts.zip <> '';

[zip2]
SELECT DISTINCT (zip1.zip) AS zip FROM zip1;

[zip3]
SELECT zip2.zip as zipCode, (SELECT COUNT(web_contacts.zip) FROM
web_contacts WHERE web_contacts.zip = zip2.zip) + (SELECT
Count(contacts.zip) FROM contacts WHERE contacts.zip = zip2.zip) AS
zipCount FROM zip2

[zip4]
SELECT TOP 50 zip3.zipCode, zip3.zipCount FROM zip3 ORDER BY zip3.zipCount;

By the time I get to executing the zip3 query for testing, it takes a
few seconds to get the first part of the data, but takes nearly 4
minutes to retrieve the whole set of 6000 records...

When I run zip4, it chugs along for 12 minutes of nothing, then up pops
an error stating that the "Object invalid or no longer set."

I then changed zip3 to a make table query, and changed zip4 to select
from that table instead. This seems to work, but zip3 still takes about
4 minutes to complete. Is there a more efficient way to handle this?

There are about 8900 records in contacts.
There are about 12500 records in web_contacts.
zip1 returns 7400 results in about 2 seconds.
zip2 returns 6000 records in about 2 seconds.

The database will only grow, and it looks like the rate is on average
500 records/week...

Thanks in Advance!

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Nov 13 '05 #1
2 1772
Justin Koivisto wrote:
Great, no problems there... However - It's never simple is it? - I now
need to do the same thing in a Microsoft Access database. I tried this:

[zip1]
SELECT distinct(web_contacts.zip) FROM web_contacts WHERE
web_contacts.zip <> '' UNION ALL SELECT DISTINCT(contacts.zip) FROM
contacts WHERE contacts.zip <> '';
Change this to a totals query. Drag the zip into 2 columns. Set to a
Totals query (inverted M on the menu). Set the first column to GroupBy,
and Count on the second zip code. Do this for each table and then Union
it like above witht the same filters.

SELECT zip, Count([Zip] As ZipCnt FROM web_contacts WHERE
zip <> '' Group By Zip UNION ALL
SELECT zip, Count([Zip] As ZipCnt FROM contacts WHERE
zip <> '' Group By Zip
[zip2]
SELECT DISTINCT (zip1.zip) AS zip FROM zip1;
Change this to a totals query. This time drag both zips, the first
group by, the second sum.

Select zip, Sum(ZipCnt) As ZipSum From Zip1 Group By Zip

[zip3]
SELECT zip2.zip as zipCode, (SELECT COUNT(web_contacts.zip) FROM
web_contacts WHERE web_contacts.zip = zip2.zip) + (SELECT
Count(contacts.zip) FROM contacts WHERE contacts.zip = zip2.zip) AS
zipCount FROM zip2
get rid of this one.

[zip4]
SELECT TOP 50 zip3.zipCode, zip3.zipCount FROM zip3 ORDER BY zip3.zipCount;


Use the results from Zip2's new Totals query. Using SubSelects is a
very slow way of doing things.

SELECT TOP 50 zipCode, zipSum FROM zip2 ORDER BY zipSum

Nov 13 '05 #2
Salad wrote:
Justin Koivisto wrote:
Great, no problems there... However - It's never simple is it? - I now
need to do the same thing in a Microsoft Access database. I tried this:

[zip1]
SELECT distinct(web_contacts.zip) FROM web_contacts WHERE
web_contacts.zip <> '' UNION ALL SELECT DISTINCT(contacts.zip) FROM
contacts WHERE contacts.zip <> '';

Change this to a totals query. Drag the zip into 2 columns. Set to a
Totals query (inverted M on the menu). Set the first column to GroupBy,
and Count on the second zip code. Do this for each table and then Union
it like above witht the same filters.

SELECT zip, Count([Zip] As ZipCnt FROM web_contacts WHERE
zip <> '' Group By Zip UNION ALL
SELECT zip, Count([Zip] As ZipCnt FROM contacts WHERE
zip <> '' Group By Zip
> [zip2]

SELECT DISTINCT (zip1.zip) AS zip FROM zip1;

Change this to a totals query. This time drag both zips, the first
group by, the second sum.

Select zip, Sum(ZipCnt) As ZipSum From Zip1 Group By Zip

[zip3]
SELECT zip2.zip as zipCode, (SELECT COUNT(web_contacts.zip) FROM
web_contacts WHERE web_contacts.zip = zip2.zip) + (SELECT
Count(contacts.zip) FROM contacts WHERE contacts.zip = zip2.zip) AS
zipCount FROM zip2

get rid of this one.

[zip4]
SELECT TOP 50 zip3.zipCode, zip3.zipCount FROM zip3 ORDER BY
zip3.zipCount;


Use the results from Zip2's new Totals query. Using SubSelects is a
very slow way of doing things.

SELECT TOP 50 zipCode, zipSum FROM zip2 ORDER BY zipSum


Thank you! GROUB BY is the key I needed... I really need to read up on
that again so I can remember it!

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2924
by: lawrence | last post by:
A very strange bug. www.monkeyclaus.org is run by a cms I'm developing. One of types of users we allow is "justTestingTheSite", a type of user I developed to give demo's to prospective clients. The...
3
5372
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
13
2182
by: Chris Mantoulidis | last post by:
There must be some tips to make a program quicker. I guess more than 50% of ppl here will say "avoid the if-s". Yeah I know this makes a program quicker but some times an "if" is inevitable,...
8
1725
by: WindAndWaves | last post by:
Hi Gurus I have a query that contains reference to the following function: Public Function UserStatus() As Byte Const ProEro = 1: 'on error GoTo ERR '--- UserStatus = Nz(DLookup("", "", "!="...
12
2342
by: Tom | last post by:
Hello, I have a database of employee data in access and I am trying to create a form with combo boxes for criteria (ex. gender, office, position, etc.) that let the user select criteria from...
7
1496
by: adam | last post by:
I'm currently coding a CMS system for a site which includes the feature to create multiple sections inside a page. To add each of these new sections I'm using DOM with AJAX to save, but I've got a...
9
2753
by: QCLee | last post by:
Sir can you help me to transfer my Access Query to MS excel? i have a command button on the form to export the parameter query named "HVACWindwardQuery" to excel spreadsheet and i got the codes...
3
9107
by: RAG2007 | last post by:
I'm using the QueryDef and Execute method to update a record in my MySQL backend. Problem: When the Passthrough update query is defined using QueryDef, it becomes a select query, and I cannot use...
20
2865
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.