473,385 Members | 1,593 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 Your Guys Help With A Query

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 query that will delete rows that have the same
"Code" and same "date" but will keep the line that has the higher
value in in the time field. Sometimes there are rows that have one
time that is lower and one that is higher, sometimes there can be 20
lines that have different times but the same higher value (ie. first
three lines of sample table above). I'm lost and have been banging my
head for the past two days. The finish table would looke like this.

Date Time Code
1/1/2003 1800 xbc
2/4/2004 1700 abc

Any help would be appreciated.

The Pig
8675309
Nov 12 '05 #1
5 2653
ia*******@yahoo.com (The Pig) wrote in message news:<81**************************@posting.google. com>...
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 query that will delete rows that have the same
"Code" and same "date" but will keep the line that has the higher
value in in the time field. Sometimes there are rows that have one
time that is lower and one that is higher, sometimes there can be 20
lines that have different times but the same higher value (ie. first
three lines of sample table above). I'm lost and have been banging my
head for the past two days. The finish table would looke like this.

Date Time Code
1/1/2003 1800 xbc
2/4/2004 1700 abc

Any help would be appreciated.

The Pig
8675309

L.S.,

Do you really have to delete the rows or is filtering them out is an
option ?
Filtering out is the simplest method:
when in design-view click the totals-button (the greek E-like symbol)
and select the max-function in stad of the group-by function on the
total-line for the time-column. The resulting query will give what you
want. The resulting query however is not updatable but perfect for
other use without deleting the table-data.
You also could save the result by changing the querytype to create a
new table through the query-menu.

Marc
Marc
Nov 12 '05 #2
Piggy,

Is there a Primary Key on the table? Some way to uniquely identify a
record like a unique constraint?

Otherwise how does the db 'know' which of the same records to delete?

Given you have a PK make 2 queries

SELECT Min(CDT2.PK) AS MinOfPK, CDT2.Code, CLng([DateTime]) AS DateOf
FROM CodeDateTime AS CDT2
WHERE (((CDT2.DateTime)=(SELECT Max(CDT3.DateTime) FROM CodeDateTime as
CDT3 WHERE clng(CDT3.DateTime) = clng(CDT2.DateTime))))
GROUP BY CDT2.Code, CLng([DateTime]);

I've called it qselPKSToKeep. This will 'arbitarily' select one of the
PK's too keep if the datetime and code are same (the lowest number but it
could be the max, it doesn't matter)

Second Query (the 'outer' query)

DELETE CodeDateTime.PK, CodeDateTime.DateTime, CodeDateTime.Code
FROM CodeDateTime
WHERE (((CodeDateTime.PK) Not In (SELECT PTK.MinOfPK FROM qselPKSToKeep
as PTK)));

There may be a simpler way but I certainly can't think of it now....

Peter


ia*******@yahoo.com (The Pig) wrote in
news:81**************************@posting.google.c om:
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 query that will delete rows that have the same
"Code" and same "date" but will keep the line that has the higher
value in in the time field. Sometimes there are rows that have one
time that is lower and one that is higher, sometimes there can be 20
lines that have different times but the same higher value (ie. first
three lines of sample table above). I'm lost and have been banging my
head for the past two days. The finish table would looke like this.

Date Time Code
1/1/2003 1800 xbc
2/4/2004 1700 abc

Any help would be appreciated.

The Pig
8675309


Nov 12 '05 #3
Here you go:

Delete *
From test as t
Where (Select Max(Time)
From Test Where test.code = t.code and test.date = t.date) > t.time

This would not handle the problem where you have two records with the
exact same time....

ia*******@yahoo.com (The Pig) wrote in message news:<81**************************@posting.google. com>...
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 query that will delete rows that have the same
"Code" and same "date" but will keep the line that has the higher
value in in the time field. Sometimes there are rows that have one
time that is lower and one that is higher, sometimes there can be 20
lines that have different times but the same higher value (ie. first
three lines of sample table above). I'm lost and have been banging my
head for the past two days. The finish table would looke like this.

Date Time Code
1/1/2003 1800 xbc
2/4/2004 1700 abc

Any help would be appreciated.

The Pig
8675309

Nov 12 '05 #4
ia*******@yahoo.com (The Pig) wrote in message news:<81**************************@posting.google. com>...
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 query that will delete rows that have the same
"Code" and same "date" but will keep the line that has the higher
value in in the time field. Sometimes there are rows that have one
time that is lower and one that is higher, sometimes there can be 20
lines that have different times but the same higher value (ie. first
three lines of sample table above). I'm lost and have been banging my
head for the past two days. The finish table would looke like this.

Date Time Code
1/1/2003 1800 xbc
2/4/2004 1700 abc

Any help would be appreciated.

The Pig
8675309

If the data type for Time is number, I think the following code works.
SELECT Date, MAX(Time) AS MaxOfTime, Code
FROM tblYourTable
GROUP BY Date, Code;

Xiaolu
Nov 12 '05 #5
You can do this by creating a temporary table of the records you want.
Then delete all of the records in the base table and repopulate it
with the wanted records. Here are the queries you can use to do this.

First select the wanted records into a temporary table:

SELECT date, Max(time) AS maxtime, code INTO temp
FROM Table2
GROUP BY date, code;
Then delete all of the records in the base table:

DELETE *
FROM Table2;

Finally repopulate the base table with the stored records:

INSERT INTO Table2
SELECT temp.*
FROM temp;
Hope this helps.

Jeff
Nov 12 '05 #6

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

Similar topics

34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
3
by: Jack A | last post by:
OK Guys. I'm fed up of the query below taking too much time. I CANT change the query since it is generated by a 3rd party product. I can change indexes and add new indexes though. The schema of...
2
by: Bobby | last post by:
Hello everyone I have a question. The school I am working for is in the beginning process of having a webpage that will direct students to download there homework and be able to view there info...
6
by: Vlad Olevsky | last post by:
Hi guys We have a following problem. For security reasons in each table in our DB we have addition field which is calculated as hash value of all columns in particular row. Every time when...
9
by: ILCSP | last post by:
Hello, perhaps you guys have heard this before in the past, but here is what I'm looking for. I have a SQL 2000 table with Social security numbers. We need to create a Member ID using the...
28
by: Gaby | last post by:
Hi guys, I have this ASP code that is a little long to post so but you can download it off of http://whatthedeuce.net/Form.asp I cant find out what my error is. I know it is not in my Access...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
3
by: mlcampeau | last post by:
Hi guys, I am trying to run a query that calculates when employees become eligible for supplemental vacation. This occcurs once the employee has reached 5, 10, 15, etc years of service. I got the...
2
by: cephal0n | last post by:
Hi All! First of I apologize for my previews post needing help on union select and not providing so more explanation, but thank you very much for your opinion and sugestion. I have thought about my...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.