473,659 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2667
ia*******@yahoo .com (The Pig) wrote in message news:<81******* *************** ****@posting.go ogle.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.DateTim e)=(SELECT Max(CDT3.DateTi me) FROM CodeDateTime as
CDT3 WHERE clng(CDT3.DateT ime) = clng(CDT2.DateT ime))))
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.Da teTime, CodeDateTime.Co de
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.goo gle.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 #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.go ogle.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.go ogle.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
7067
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. Yensao
3
1795
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 the tables is given below. The most expensive operation is a bookmark lookup on VGNCCB_ROLE_JT. I created the speed_up_login index as a covering index to cover the query but that has not seemed to help. Any ideas, suggestions are most welcome...
2
1928
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 like test scores and etc(the homework and info page will reside on our webservers at the school on the local intranet network). Now what I need is a way for the students to go to a login page and when logging in will be automatically directed to...
6
1143
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 some field in particular row is changed we create and call select query from our application to obtain all fields for this row and then re-calculate and update the hash value again.
9
8580
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 Member's real SSN but since we are not allowed to use the exact SSN, we need to add 1 to each number in the SSN. That way, the new SSN would be the new Member ID. For example:
28
2110
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 because I have tested my query and it works fine. but whenever i try to submit from the page the imformation doesnt go thorugh. on a side note, if i hit refresh it tells me it cannot refresh without sending the information. i dont know if this...
8
4989
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 string and print it. but i want to use the String.Format() method if possible to do it.
3
2147
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 query running great, it shows the correct dates, (5, 10, etc years after the HireDate) but I just found out that they don't want to know the exact anniversary date. People become entitled to their vacation January 1st of the year of their anniversary...
2
1484
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 problem and try to find a solution. I now have few answres and would like to ask for your expertise on how I handled my sql. I made an sql code, I used sql specific by creating query > new > design view> close the Show Table > right click> click...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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
8630
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...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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
4176
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
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
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

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.