473,396 Members | 2,113 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,396 software developers and data experts.

Query In Error?

Hi There,

I can't seem to see what's wrong with the query below

DELETE
FROM Users_Details UD1
WHERE UD1.UserID = (
SELECT TOP 1 UD2.UserID
FROM Users_Details UD2
WHERE UD1.useremail = UD2.useremail
)

Keeps giving me incorrect syntax on Line 1 near UD1.
Can you not alias tables when using a Delete?
What I'm trying to do is cleanse the table of duplicate e-mail
addresses but I always want to leave one copy of the e-mail address
i.e. if there are 3 of the same e-mail address then I want to remove 2
of them.
Anyone got a better way of doing it that doesn't use cursors then I
would appreciate it.

Cheers
Jul 20 '05 #1
4 2346
wa**********@hotmail.com (Wanny) wrote in message news:<e5**************************@posting.google. com>...
Hi There,

I can't seem to see what's wrong with the query below

DELETE
FROM Users_Details UD1
WHERE UD1.UserID = (
SELECT TOP 1 UD2.UserID
FROM Users_Details UD2
WHERE UD1.useremail = UD2.useremail
)

Keeps giving me incorrect syntax on Line 1 near UD1.
Can you not alias tables when using a Delete?
What I'm trying to do is cleanse the table of duplicate e-mail
addresses but I always want to leave one copy of the e-mail address
i.e. if there are 3 of the same e-mail address then I want to remove 2
of them.
Anyone got a better way of doing it that doesn't use cursors then I
would appreciate it.

Cheers


Sometimes an error message is not as cryptic as it seems and actually
very helpfull. If you check SQL books online for the definition of the
DELETE statement you will notice that you can only use table aliasses
in an optional seperate FROM clause.

Fortunatly this error was a blessing in disguise as your statement
will also purge all users without a duplicate e-mail adress from the
system. Your correlated subquery fails to check that it's not actually
matching the user from the main query.

So let's try to create something that might work a little better.
Since you didn't post any DDL I'm going to guess a little, but you'll
get the idea. I'll use a subselect which will will select those
userid's to be deleted (I'm keeping the userid's with the largest
value, under the assumption that those are the most recent ones)

create table user_details
(
UserID int not null,
Useremail varchar(255) not null
)

insert into user_details values
(1,'a**@def.gh')
insert into user_details values
(2,'i**@lmn.op')
insert into user_details values
(3,'q**@tuv.wx')
insert into user_details values
(4,'a**@def.gh')
insert into user_details values
(5,'i**@lmn.op')
insert into user_details values
(6,'f**@bar.org')
insert into user_details values
(7,'a**@def.gh')

DELETE FROM
user_details where userid in
(
select
Distinct ToBeDeleted.userid
FROM
user_details ToBeDeleted inner join user_details ToBeKept
on ToBeDeleted.UserID < ToBeKept.UserID and ToBeDeleted.UserEmail =
ToBeKept.UserEmail
)
With Regards
Andre Kuyt
Jul 20 '05 #2
Hi Andre,

Thanks a lot for your help.
Your query did exactly what I needed.
I don't quite understand how the query works though and would
appreciate it if you could help me out further.
The bit I don't understand is, how does the query know to remove items
with a UserID less than the MAX(UserID).

We have the Join criteria

"on ToBeDeleted.UserID < ToBeKept.UserID"

but it doesn't specify anywhere that it's to be less than the
MAX(UserID) so how does it do it?

I have to apologise for my ignorance if the answer is obvious and
sorry for pestering you further.

Thanks.


So let's try to create something that might work a little better.
Since you didn't post any DDL I'm going to guess a little, but you'll
get the idea. I'll use a subselect which will will select those
userid's to be deleted (I'm keeping the userid's with the largest
value, under the assumption that those are the most recent ones)

create table user_details
(
UserID int not null,
Useremail varchar(255) not null
)

insert into user_details values
(1,'a**@def.gh')
insert into user_details values
(2,'i**@lmn.op')
insert into user_details values
(3,'q**@tuv.wx')
insert into user_details values
(4,'a**@def.gh')
insert into user_details values
(5,'i**@lmn.op')
insert into user_details values
(6,'f**@bar.org')
insert into user_details values
(7,'a**@def.gh')

DELETE FROM
user_details where userid in
(
select
Distinct ToBeDeleted.userid
FROM
user_details ToBeDeleted inner join user_details ToBeKept
on ToBeDeleted.UserID < ToBeKept.UserID and ToBeDeleted.UserEmail =
ToBeKept.UserEmail
)
With Regards
Andre Kuyt

Jul 20 '05 #3
wa**********@hotmail.com (Wanny) wrote in message news:<e5*************************@posting.google.c om>...
Hi Andre,

Thanks a lot for your help.
Your query did exactly what I needed.
I don't quite understand how the query works though and would
appreciate it if you could help me out further.
The bit I don't understand is, how does the query know to remove items
with a UserID less than the MAX(UserID).

We have the Join criteria

"on ToBeDeleted.UserID < ToBeKept.UserID"

but it doesn't specify anywhere that it's to be less than the
MAX(UserID) so how does it do it?

I have to apologise for my ignorance if the answer is obvious and
sorry for pestering you further.

Thanks.


So let's try to create something that might work a little better.
Since you didn't post any DDL I'm going to guess a little, but you'll
get the idea. I'll use a subselect which will will select those
userid's to be deleted (I'm keeping the userid's with the largest
value, under the assumption that those are the most recent ones)

create table user_details
(
UserID int not null,
Useremail varchar(255) not null
)

insert into user_details values
(1,'a**@def.gh')
insert into user_details values
(2,'i**@lmn.op')
insert into user_details values
(3,'q**@tuv.wx')
insert into user_details values
(4,'a**@def.gh')
insert into user_details values
(5,'i**@lmn.op')
insert into user_details values
(6,'f**@bar.org')
insert into user_details values
(7,'a**@def.gh')

DELETE FROM
user_details where userid in
(
select
Distinct ToBeDeleted.userid
FROM
user_details ToBeDeleted inner join user_details ToBeKept
on ToBeDeleted.UserID < ToBeKept.UserID and ToBeDeleted.UserEmail =
ToBeKept.UserEmail
)
With Regards
Andre Kuyt


A little further clarification seems to be in order, so lets move back
to the original example data and try this little query to see what it
actually is that it is selecting:

select * from user_details ToBeDeleted inner join user_details
ToBeKept
on ToBeDeleted.UserID < ToBeKept.UserID and ToBeDeleted.UserEmail =
ToBeKept.UserEmail
order by toBeDeleted.userid

This yields the following resultset

UserID UserEmail UserID Useremail
----------- ------------ ----------- ----------
1 ab*@def.gh 4 ab*@def.gh
1 ab*@def.gh 7 ab*@def.gh
2 ij*@lmn.op 5 ij*@lmn.op
4 ab*@def.gh 7 ab*@def.gh

So what we see is that the condition ToBeDeleted.UserID <
ToBeKept.UserID
actually results in at least one row in the resultset for each userid
which has at least one higher ranking userid with the same email
address.

UserID 1 actually appears twice, because it has 2 higher userids with
the same email address (4 and 7)

So what we know from this resultset is the following: If a userid
appears in the left most column, there is at least 1 userid with a
higher number in the table. As you can see for yourself UserID 7 is
excluded from the ToBeDeleted list because there is no larger userID
available. For that same reason all userid's without duplicate emails
are excluded from the ToBeDeleted results.

So if you remove all the rows with the ToBeDeleted UserID's from the
table you will be left with those records without duplicates which
have the Maximum userID

So, as you can see, the query itself actually knows nothing about a
MAX(UserID), which isn't surprising at all as queries have no brains
:)

(Advanced topic:
As a variation of this query you could use the join condition
ToBeDeleted.UserID > ToBeKept.UserID to keep all the minimum UserID's.
)
Andre
Jul 20 '05 #4
Hi Andre,

Thanks again.
The explanation was excellent and I'm well on my way to being an
expert SQL programmer ;-)

Cheers
Jul 20 '05 #5

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

Similar topics

1
by: Stijn Goris | last post by:
I get an error when trying to insert a new row: query error- 5 - Duplicate entry '80.200.213.102 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Avant Browser ; MyIE2; .NET CLR 1.0.' for key 1...
2
by: Jim Hatfield | last post by:
I'm using: "select min(objid) from table_name where objid > $objid" to select the next row in a sequence where there may be gaps. It works fine unless the where clause results in an empty set. ...
1
by: Dalan | last post by:
I can't seem to find a workaround of Query Syntax Error. Actually, the query performs just fine, except when the last record on a related subform is deleted, then it generates a Runtime Error 3075...
2
by: Jean | last post by:
Hello everyone, I was having the following problem with a query, and after failing to find a similar solution on these newsgroups I decided to post here. I am quite new to Access, so would...
3
by: jallegue | last post by:
I am working with MS-Access 2002. The two tables that I am working with are: dbo_IDX_FRS_account_bal_by_month ==> this is a linked table to SQL == local table The query that is executed is...
5
by: Annie | last post by:
hello guys, I have little experience working with C# and MS Access ... I am having an insert query with one datetime field and a boolean and couple of text and number fields as below: ...
4
by: franc sutherland | last post by:
Hello, I am using Access 2003. I am having trouble trapping the "can't append all the records in the append query" error message when appending data to a query from a table which is linked to...
8
Cintury
by: Cintury | last post by:
The problem is I have a function that I've created and stored in a module. I call it as an expression (e.g. total: Function(parameter)). I'm receiving the error 3061: too few parameters, expected 1....
12
by: lupis | last post by:
I am creating a query to find unmatched rows between a local access table and an online MS SQL Server table based on the PK field ID. This is working without any flaws. My problems start when I run a...
2
MindBender77
by: MindBender77 | last post by:
Hello again, All... Admin's, I wasn't sure where to post this, please move if in wrong forum. I'm using FrontPage 2003 to post form data to a second form to be used in an update query. The query...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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,...

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.