473,385 Members | 1,876 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,385 software developers and data experts.

Need help with delete query

Hello, I have a 2 column file that looks like this:

VALUE1 | VALUE2
12 | 7
10 | 7
9 | 8
10 | 8
11 | 8
6 | 9
8 | 9
9 | 9
What I need to do is delete any row when there is a more than one value in
VALUE2 that is the same and only keep the incidence with the highest value
in VALUE1.

Hmm.. that doesn't explain very well. Maybe this will help..

For example the result on the above should wind up being:

VALUE1 | VALUE2
12 | 7
11 | 8
9 | 9

So I wind up with only unique values in VALUE2 and the ones that are tossed
are the duplicates with VALUE1 values lower than the highest one found.

Does that make sense? And if so how can I do that?

Thanks.
Nov 12 '05 #1
3 1809
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

2 ways:

Way 1:

Run a make-table query that gets the data you want, then drop the
original table & rename the new table the old table.

Make-table query:

SELECT Max(Value1) As MaxValue1, Value2 INTO NewTableName
FROM OldTableName
GROUP BY Value2

DDL statement to drop the old table:

DROP TABLE OldTable

Rename the table using VBA:

DoCmd.Rename "OldTableName", acTable, "NewTableName"

====================

Way 2 (untested):

Run query like this:

DELETE *
FROM TableName
WHERE NOT EXISTS (SELECT Max(Value1) As MaxValue1, Value2
FROM TableName
GROUP BY Value2

To see if this works substitute SELECT for DELETE, run it & see if the
results are what you want deleted.
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQDPGaoechKqOuFEgEQLiigCfT1cAlWsdarZDvrxT1mjLQz oK2JgAoOvh
91SgP58XdPJMyXfXWjMPMpDL
=W8Ug
-----END PGP SIGNATURE-----
Adam wrote:
Hello, I have a 2 column file that looks like this:

VALUE1 | VALUE2
12 | 7
10 | 7
9 | 8
10 | 8
11 | 8
6 | 9
8 | 9
9 | 9
What I need to do is delete any row when there is a more than one value in
VALUE2 that is the same and only keep the incidence with the highest value
in VALUE1.

Hmm.. that doesn't explain very well. Maybe this will help..

For example the result on the above should wind up being:

VALUE1 | VALUE2
12 | 7
11 | 8
9 | 9

So I wind up with only unique values in VALUE2 and the ones that are tossed
are the duplicates with VALUE1 values lower than the highest one found.

Does that make sense? And if so how can I do that?


Nov 12 '05 #2
Along similar lines, can you suggest a way to merge 5 records in a table into
one master record containing all the data in the fields of the five records?

Each of the 5 records contains firstname, lastname, city, state and zip in
separate fields. There is no primary key in the records. All the records contain
the same 15 other fields. In each of these 15 fields (for example SSN) one or
more records may contain data or that field could be blank in all records. For
the moment we will assume that if multiple records contain data in a field,
there are no conflicts between the records. Do you have any suggestion on how to
merge all five records into one master record where each of the 15 fields would
contain data if the data for that field existed in any of the 5 records?

Thanks for any help you can give me!

Mark
"MGFoster" <me@privacy.com> wrote in message
news:rD*****************@newsread1.news.pas.earthl ink.net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

2 ways:

Way 1:

Run a make-table query that gets the data you want, then drop the
original table & rename the new table the old table.

Make-table query:

SELECT Max(Value1) As MaxValue1, Value2 INTO NewTableName
FROM OldTableName
GROUP BY Value2

DDL statement to drop the old table:

DROP TABLE OldTable

Rename the table using VBA:

DoCmd.Rename "OldTableName", acTable, "NewTableName"

====================

Way 2 (untested):

Run query like this:

DELETE *
FROM TableName
WHERE NOT EXISTS (SELECT Max(Value1) As MaxValue1, Value2
FROM TableName
GROUP BY Value2

To see if this works substitute SELECT for DELETE, run it & see if the
results are what you want deleted.
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQDPGaoechKqOuFEgEQLiigCfT1cAlWsdarZDvrxT1mjLQz oK2JgAoOvh
91SgP58XdPJMyXfXWjMPMpDL
=W8Ug
-----END PGP SIGNATURE-----
Adam wrote:
Hello, I have a 2 column file that looks like this:

VALUE1 | VALUE2
12 | 7
10 | 7
9 | 8
10 | 8
11 | 8
6 | 9
8 | 9
9 | 9
What I need to do is delete any row when there is a more than one value in
VALUE2 that is the same and only keep the incidence with the highest value
in VALUE1.

Hmm.. that doesn't explain very well. Maybe this will help..

For example the result on the above should wind up being:

VALUE1 | VALUE2
12 | 7
11 | 8
9 | 9

So I wind up with only unique values in VALUE2 and the ones that are tossed
are the duplicates with VALUE1 values lower than the highest one found.

Does that make sense? And if so how can I do that?

Nov 12 '05 #3
DoCmd.RunSQL "Delete * From tbl1 Where Exists " _
& "(Select * From tbl1 As t Where val2 = tbl1.val2 " _
& "And val1 > tbl1.val1)"
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #4

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

Similar topics

2
by: r | last post by:
I am trying to do a delete query like this one: DELETE FROM pending WHERE pages.url=pending.url AND pages.date=pending.date; If I run it (via PHP) I get the following error: Unknown table...
5
by: The Pig | last post by:
Got a table. Date Time Code 1/1/2003 1700 xbc 1/1/2003 1800 xbc 1/1/2003 1800 xbc 2/4/2004 1650 abc 2/4/2004 1700 abc The problem is I need a...
2
by: Dalan | last post by:
Having searched the archives regarding a Delete Query, I found nothing specific to my need, although there seems to be a plethora of ideas and suggestions on queries in general. I have used Delete...
5
by: ms | last post by:
Why does this select query return the correct records but when I make it a delete query I get a msgbox with "Could not delete from specified tables". SELECT BMIDLog.* FROM stageBMIDLog INNER...
3
by: White Bilky | last post by:
A97. Simplified description: I have a query which returns one field containing numbers. I have a table where one field contains the same numbers as the query (amongst others). The query and...
13
by: forbes | last post by:
Hi, I have a user that used the Query Wizard to create a query in Access. Now she claims that her master table is missing all the data that was excluded from the query. Can you create anything...
6
by: Hemant Shah | last post by:
Folks, I am having trouble with a query. DB2 does not use index, it does relation scan of the table. I am running DB2 UDB 8.2 on Fedora Core release 4 (Stentz) # db2level DB21085I ...
3
Odisey
by: Odisey | last post by:
I am not sure if this is a MySQL syntax error or a DB issue yet. I am getting the error from the code: Page Error! This page has been accessed in error. The GET is populated in the browser...
10
by: pythonnoob | last post by:
Hello everyone. New to python as well as this forum, but i must say ive learned a but already reading through some posts. Seems to be a pretty helpful community here. Before i post a question...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.