473,614 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Syntax

I have an Access 97 database on WinXP with a linked Excel table of about
100,000 rows.
I'm trying to construct a query that will give me an editable datasheet
showing records where data in a pair of fields is duplicated.

As an example, I constructed this query on the Employees'table in the
Northwind Sample database. I appended four rows from the table back to the
table creating duplicates, then ran this SQL to locate rows where Firstname
and LastName together were duplicated.

SELECT Employees.*
FROM Employees
WHERE [LastName] & [FirstName] In (
SELECT [LastName] & [FirstName] AS FullName
FROM Employees
GROUP BY [LastName] & [FirstName]
HAVING Count([LastName] & [FirstName])>1)
AND Employees.Title OfCourtesy='Mr. ';

The final AND condition is actually a Date in my "real" project. LastName
and FirstName are Product Labels and ID Numbers. A query that works as
above will work on my real data.

This gives me exactly what I want, and I can add and change fields as
required (and it will be required.)

However, it takes (on my machine) about 10 seconds to run on the real data.
By creating and saving two separate queries, then constructing a third
query JOINing on FullName I can see the same result in 4 seconds.

So, my question.
Is there a different syntax that will give me the same (or better!) speed
in ONE query?
I'm aware that I can join on a Select statement instead of a Table or query
by enclosing the select statement in square brackets followed by a dot,
even in 97. My gut feeling is that there's a way I can use that fact, but
just at the moment my gut isn't talking to my brain.

I'd be grateful for any help.

Cheers,
DBDriver
May 9 '07 #1
6 3597
I'm aware that I can join on a Select statement instead of a Table or query
by enclosing the select statement in square brackets followed by a dot,
even in 97. My gut feeling is that there's a way I can use that fact,
you can add a derived table to your query in the form of a sub query
in SQL - Access messes this up once you view it in design mode and
converts the proper bracketing of the sub query with square brackets
and dot. eg (air code)

Select
Employees.name,
qryDuplicates.F ullName
From
Employees Inner Join
(SELECT
FirstName, LastName,
[LastName] & [FirstName] AS FullName
FROM Employees
GROUP BY [LastName] & [FirstName]
HAVING Count([LastName] & [FirstName])>1
AND Employees.Title OfCourtesy='Mr. ';
) as qryDuplicates
On ((tblEmpoyees.F irstName = qryDuplicates.F irstName
AND (tblEmployees.L astName = qryDuplication. LastName))
once you view this in the design mode it will change it to:

Select
Employees.name,
qryDuplicates.F ullName
From
Employees Inner Join
[SELECT
FirstName, LastName,
[LastName] & [FirstName] AS FullName
FROM Employees
GROUP BY [LastName] & [FirstName]
HAVING Count([LastName] & [FirstName])>1
AND Employees.Title OfCourtesy='Mr. ';
]. as qryDuplicates
On ((tblEmpoyees.F irstName = qryDuplicates.F irstName
AND (tblEmployees.L astName = qryDuplication. LastName))

....and then it won't recompile!!! maybe i'm missing something here -
but I always found that odd.

you could do that here, but it wouldn't be any faster. In fact it may
actually be slower then having a seperately compiled query joined. And
it wouldn't be updatable either... so no joy there

You may want to try compiling the dupilcate query seperately, but
using the same technique of using the "Where Field In" filter - I'm
not sure how the jet sql engine works, but I'd worry it was re-
evaluating that SQL expression on for each record of the Employess
table; try:

qryEmployees:=
SELECT Employees.*
FROM Employees
WHERE [LastName] & [FirstName] In (qryDuplicates)
AND Employees.Title OfCourtesy='Mr. ';

qryDuplicates:=
SELECT [LastName] & [FirstName] AS FullName
FROM Employees
GROUP BY [LastName] & [FirstName]
HAVING Count([LastName] & [FirstName])>1

....may not make a difference, but worth a shot :)

Also for speed, never use "select tbl.*" - naming the fields is faster

just at the moment my gut isn't talking to my brain.
ha! story of my life - but mostly its my liver not talking to
anyone... whoever said Guinness was good for you!

May 9 '07 #2

just noticed i messed up my copy & pasting - that _should_ have been:
qryEmployees:=
SELECT Employees.*
FROM Employees
WHERE [LastName] & [FirstName] In (qryDuplicates) ;

qryDuplicates:=
SELECT [LastName] & [FirstName] AS FullName
FROM Employees
GROUP BY [LastName] & [FirstName]
HAVING Count([LastName] & [FirstName])>1
AND Employees.Title OfCourtesy='Mr. ';
....much better!

May 9 '07 #3
Thanks for looking at this, BillCo

I found that Access complained about the AND clauee in qryDuplicates,
telling me it should have been part of an Aggregate function.

I found that qryEmployees considered qryDuplicates to be a Parameter.
(Testing in Win98 and Access 97)

It's been an interesing exersize.

Cheers,
DBDriver

BillCo <co**********@g mail.comwrote in
news:11******** **************@ l77g2000hsb.goo glegroups.com:
>
just noticed i messed up my copy & pasting - that _should_ have been:
>qryEmployees :=
SELECT Employees.*
FROM Employees
WHERE [LastName] & [FirstName] In (qryDuplicates) ;

qryDuplicates: =
SELECT [LastName] & [FirstName] AS FullName
FROM Employees
GROUP BY [LastName] & [FirstName]
HAVING Count([LastName] & [FirstName])>1
AND Employees.Title OfCourtesy='Mr. ';

...much better!

May 9 '07 #4
you can add a derived table to your query in the form of a sub query
in SQL - Access messes this up once you view it in design mode and
converts the proper bracketing of the sub query with square brackets
and dot. eg (air code)
I hadn't noticed that. Thanks!
>
you could do that here, but it wouldn't be any faster. In fact it may
actually be slower then having a seperately compiled query joined. And
it wouldn't be updatable either... so no joy there
Update is essential. I've created some quite interesting constructions
which weren't uptatable (and therefore no use to me.)
You may want to try compiling the dupilcate query seperately, but
using the same technique of using the "Where Field In" filter - I'm
not sure how the jet sql engine works, but I'd worry it was re-
evaluating that SQL expression on for each record of the Employess
table; try:

qryEmployees:=
SELECT Employees.*
FROM Employees
WHERE [LastName] & [FirstName] In (qryDuplicates)
AND Employees.Title OfCourtesy='Mr. ';

qryDuplicates:=
SELECT [LastName] & [FirstName] AS FullName
FROM Employees
GROUP BY [LastName] & [FirstName]
HAVING Count([LastName] & [FirstName])>1

...may not make a difference, but worth a shot :)
(Comment in next message.)
>
Also for speed, never use "select tbl.*" - naming the fields is faster
You're right, but in this case I'm making a semi-template to get various
duplicated entries in multiple columns from a 90 column Excel sheet.

I was really just curious about the []. method. My method worked quite well
for me, but there's always someone out there with a skillful tweak.

Cheers,
DBDriver
May 9 '07 #5
On May 9, 1:24 pm, DBDriver <DBDri...@no.co wrote:
Thanks for looking at this, BillCo

I found that Access complained about the AND clauee in qryDuplicates,
telling me it should have been part of an Aggregate function.

I found that qryEmployees considered qryDuplicates to be a Parameter.
(Testing in Win98 and Access 97)

It's been an interesing exersize.

Cheers,
DBDriver
here's where it gets tricky - it's so hard to advise without seeing
first hand. Try changing qryDuplicates to:

SELECT [LastName] & [FirstName] AS FullName
FROM Employees
WHERE Employees.Title OfCourtesy='Mr. '
GROUP BY [LastName] & [FirstName]
HAVING Count([LastName] & [FirstName])>1

Actually, try to make this one work aswell (i'm curious after writing
the thing!):

Select
Employees.name,
qryDuplicates.F ullName
From
Employees Inner Join
(SELECT
FirstName, LastName,
[LastName] & [FirstName] AS FullName
FROM Employees
GROUP BY [LastName] & [FirstName]
WHERE Employees.Title OfCourtesy='Mr. '
HAVING Count([LastName] & [FirstName])>1
) as qryDuplicates
On ((tblEmpoyees.F irstName = qryDuplicates.F irstName
AND (tblEmployees.L astName = qryDuplication. LastName))

some final tips:
- You may have to go back to basics and analyse what you are trying to
achieve here... is there a better way to get there? Maybe stop
duplicates getting in to begin with.
- you may wish to revisit your use of first and last names as a
combined unique identifier. if there is no naturally absolutely unique
entity to use, then consider a syntetic primary key
- If you absolutely need a fast updatable query (and my experience
tells me that exposing users to data tables is not a good plan - even
masked by a bound form), consider filling a temporary table on the fly
and joining it on your main table to reduce the results returned...
speedville!

May 9 '07 #6
Comments inline.

BillCo <co**********@g mail.comwrote in
news:11******** **************@ w5g2000hsg.goog legroups.com:
On May 9, 1:24 pm, DBDriver <DBDri...@no.co wrote:
>Thanks for looking at this, BillCo

I found that Access complained about the AND clauee in qryDuplicates,
telling me it should have been part of an Aggregate function.

I found that qryEmployees considered qryDuplicates to be a Parameter.
(Testing in Win98 and Access 97)

It's been an interesing exersize.

Cheers,
DBDriver

here's where it gets tricky - it's so hard to advise without seeing
first hand.
Very true. As I said:
"The final AND condition is actually a Date in my "real" project. LastName
and FirstName are Product Labels and ID Numbers. A query that works as
above will work on my real data."

In this case, the data didn't matter. My interest was in different ways of
structuring the queries and subQueries.
I've always managed to get exactly what I want using multiple queries. In a
few cases I've found that a MakeTable was needed if I wanted same-day
results. Often the single query has been slower.
Try changing qryDuplicates to:
SELECT [LastName] & [FirstName] AS FullName
FROM Employees
WHERE Employees.Title OfCourtesy='Mr. '
GROUP BY [LastName] & [FirstName]
HAVING Count([LastName] & [FirstName])>1
My analogy (TitleOfCourtes y here = a unique Year in my "real" data) is not
good. The most common criterion will be that field, as duplications in
separate Years are OK while duplications within a Year are not.

The query I used in my original message is fine.
I was just curious about a join using the []. method, since I would use an
actual query instead.
Actually, try to make this one work aswell (i'm curious after writing
the thing!):
Sorry, still won't work for me. :-)
Office 97 on Win98 and Office 2003 on XP.

Error message:
Syntax Error in FROM Clause.

I noticed and changed qryDuplication, which of course made no difference.
Select
Employees.name,
qryDuplicates.F ullName
From
Employees Inner Join
(SELECT
FirstName, LastName,
[LastName] & [FirstName] AS FullName
FROM Employees
GROUP BY [LastName] & [FirstName]
WHERE Employees.Title OfCourtesy='Mr. '
HAVING Count([LastName] & [FirstName])>1
) as qryDuplicates
On ((tblEmpoyees.F irstName = qryDuplicates.F irstName
AND (tblEmployees.L astName = qryDuplication. LastName))

some final tips:
- You may have to go back to basics and analyse what you are trying to
achieve here... is there a better way to get there? Maybe stop
duplicates getting in to begin with.
That's the stage I'm trying to reach. The current query is to help me
prepare the Excel sheet for its new and exciting career as an Access
Database.
- you may wish to revisit your use of first and last names as a
combined unique identifier. if there is no naturally absolutely unique
entity to use, then consider a syntetic primary key
That's just my analogy. My "real" data is more relevant.
- If you absolutely need a fast updatable query (and my experience
tells me that exposing users to data tables is not a good plan - even
masked by a bound form), consider filling a temporary table on the fly
and joining it on your main table to reduce the results returned...
speedville!
You are absolutely right - the Temp table method is a good one.

Slight shift of approach - here's the *structure* of the kind of thing I
was attempting to explore;

A table of File information, obvious fields, DirID for my use.

tblDirInfo:
ID, AutoNumber, primary key
DirID, Long
FileName, Text
FileSize, Long
FileDate, Date/Time

This Query on that table:

SELECT DX.ID, DX.DirID, DX.FileName, DX.FileSize, DX.FileDate
FROM tblDirInfo AS DX INNER JOIN
[SELECT FileName, FileSize, FileDate
FROM tblDirInfo
GROUP BY FileName, FileSize, FileDate
HAVING (COUNT(*) = 1)].
AS Virt ON DX.FileName = Virt.FileName
ORDER BY DX.FileDate;

Look at that in Design View as an example of the kind of "virtual tables
through subqueries" picture I had in my head.

I shouldn't have used the Employees Table to illustrate what I wanted,
because although it provides some handy data for testing, there's a shift
of emphasis when looking at false data.

I mapped my Year info onto the TitleOfCourtesy field.
When you look at a field which can contain values such as Mr, Mrs, Miss,
Master, MZ, Hey You and Plenipotetatiss imuss you tend to consider it
unimportant in terms of Grouping. How often, after collecting 20 years of
data would you need to group People information by TitleOfCourtesy , rather
than by Year?

Thanks for your interest.

Cheers,
DBDriver.
May 10 '07 #7

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

Similar topics

699
33825
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
22
3413
by: Tuang | last post by:
I'm checking out Python as a candidate for replacing Perl as my "Swiss Army knife" tool. The longer I can remember the syntax for performing a task, the more likely I am to use it on the spot if the need arises. If I have to go off and look it up, as I increasingly have to do with Perl's ever hairier syntax, I'm more likely to just skip it, making me even less likely to remember the syntax the next time. So I hear that Python is easier...
14
2297
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def __init__(self, name):
16
2596
by: George Sakkis | last post by:
I'm sure there must have been a past thread about this topic but I don't know how to find it: How about extending the "for <X> in" syntax so that X can include default arguments ? This would be very useful for list/generator comprehensions, for example being able to write something like: instead of the less elegant explicit loop version that has to check for the length of each sequence. What do you think ? George
23
2518
by: Carter Smith | last post by:
http://www.icarusindie.com/Literature/ebooks/ Rather than advocating wasting money on expensive books for beginners, here's my collection of ebooks that have been made freely available on-line by their authors. There are lots of them out there but this selection cuts out the junk. If you know of any other good books that are freely available please post a link to them here and I'll consider adding them to the site.
19
2958
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $ Last-Modified: $Date: 2003/09/22 04:51:49 $ Author: Nicolas Fleury <nidoizo at gmail.com> Status: Draft Type: Standards Track
4
3778
by: Jeremy Yallop | last post by:
Looking over some code I came across a line like this if isalnum((unsigned char)c) { which was accepted by the compiler without complaint. Should the compiler have issued a diagnostic in this case? (I think it's not required to, but I'd like confirmation). Jeremy.
177
6974
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are programming languages still being designed with C's syntax? These questions drive me insane. Every waking minute...
4
7612
by: Bob hotmail.com> | last post by:
Everyone I have been spending weeks looking on the web for a good tutorial on how to use regular expressions and other methods to satisfy my craving for learning how to do FAST c-style syntax highlighting in C# but I have yet to find anything useful I know there are people at MS that know this stuff like the front of their hand and I know there are many people out on the web that are proficient in doing this as well but it seems nobody...
3
16222
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very strange error. In your experience, where I should looking to find the real error? Surely the sintax of glut is correct... gcc.exe -c glut_bitmap.c -o glut_bitmap.o -I"C:/Dev-Cpp/include" -I"../../include" -D__GNUWIN32__ -W -DWIN32 -DNDEBUG...
0
8198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8591
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8444
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7115
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5549
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2575
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 we have to send another system
1
1758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.