dbTalk Databases Forums  

Delete recordsets with same Date and Line

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Delete recordsets with same Date and Line in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Julia Härtfelder via SQLMonster.com
 
Posts: n/a

Default Delete recordsets with same Date and Line - 05-17-2005 , 07:09 AM






Hi All!

I need help with a Statement!
I am working with an Access2000 DB.
I have the following Problem.


ChNr Linie Datum Code 39 Stückzahl BHL1 BHL2 BMRH
582-064 L2.10 08.03.2005 02:30:00 FCAA 1482 17 0 1
582-064 L2.10 08.03.2005 02:30:00 FCAA 1487 17 0 1
582-114 L2.12 08.03.2005 01:00:00 FAC8 2750 12 4 0
582-114 L2.12 08.03.2005 01:00:00 FAC8 2744 12 4 0
582-114 L2.12 08.03.2005 01:00:00 FAC8 2750 12 4 0
582-094 L2.7 07.03.2005 19:45:00 FAE7 4323 4 8 1
582-094 L2.7 07.03.2005 19:45:00 FAE7 4489 4 8 1
582-094 L2.7 07.03.2005 19:45:00 FAE7 4489 4 8 1
581-294 L2.8 07.03.2005 18:20:00 FA8V 2658 2 2 1
581-294 L2.8 07.03.2005 18:20:00 FA8V 2652 2 2 1
581-294 L2.8 07.03.2005 18:20:00 FA8V 2658 2 2 1
582-114 L2.12 07.03.2005 17:45:00 FAAR 2072 12 3 6
As you can see I have a few recordsets that are double. The Thing is, there
is an ID that makes them different.

I need a Statement that deletes the surplus records where 'Datum' and
'Linie' are identical to another record. 1 record has to remain of course.
I thought of something like this.

DELETE FROM tbAuswert
WHERE EXISTS(
SELECT *
FROM tbAuswert
WHERE (Linie AND Datum)
IN (
SELECT Linie AND Datum AS Suchkrit
FROM tbAuswert
GROUP BY Suchkrit
HAVING ((Count(Suchkrit)>1)
)
)

But I get an error:
You wanted to execute a Query that did not have the following expression
'Not Linie = 0 And Not Datum = 0' in its Aggregatefunction

Perhaps you ccan help me.

Thanks
Julia

--
Message posted via http://www.sqlmonster.com

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Delete recordsets with same Date and Line - 05-17-2005 , 09:27 AM






Julia Härtfelder via SQLMonster.com (forum (AT) nospam (DOT) SQLMonster.com) writes:
Quote:
I need help with a Statement!
I am working with an Access2000 DB.
...
I need a Statement that deletes the surplus records where 'Datum' and
'Linie' are identical to another record. 1 record has to remain of course.
I thought of something like this.

DELETE FROM tbAuswert
WHERE EXISTS(
SELECT *
FROM tbAuswert
WHERE (Linie AND Datum)
IN (
SELECT Linie AND Datum AS Suchkrit
FROM tbAuswert
GROUP BY Suchkrit
HAVING ((Count(Suchkrit)>1)
)
)

But I get an error:
You wanted to execute a Query that did not have the following expression
'Not Linie = 0 And Not Datum = 0' in its Aggregatefunction

Perhaps you ccan help me.
If you don't have a primary that uniquely identifies each row, then
this will be very difficult, as SQL is designed to operate only from the
data in the tables.

It's possible that you can add a column that gives you a unique ID. Had
you been using SQL Server, I could have showed you how. However, since
you are using Access, you are better off posting your question to a
forum for Access. There are considerable differences between the SQL in
SQL Server and Access.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


Reply With Quote
  #3  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Delete recordsets with same Date and Line - 05-17-2005 , 04:53 PM



On Tue, 17 May 2005 12:09:04 GMT, Julia Härtfelder via SQLMonster.com
wrote:

Quote:
Hi All!

I need help with a Statement!
I am working with an Access2000 DB.
I have the following Problem.
(snip)
As you can see I have a few recordsets that are double. The Thing is, there
is an ID that makes them different.

I need a Statement that deletes the surplus records where 'Datum' and
'Linie' are identical to another record. 1 record has to remain of course.
Hi Julia,

The following will work for SQL Server. I'm not sure about Access though
(it deviates from the standard in other ways than SQL Server does <g>),
so test it first, and repost in an Access group if this doesn't work.

Note: I assume that the ID column is called "ID" and that you want to
retain the row with the lowest ID value.

DELETE FROM tbAuswert
WHERE EXISTS
(SELECT *
FROM tbAuswert AS a2
WHERE a2.Datum = tbAuswert.Datum
AND a2.Linie = tbAuswert.Linie
AND a2.ID > tbAuswert.ID)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Reply With Quote
  #4  
Old   
Julia Härtfelder via SQLMonster.com
 
Posts: n/a

Default Re: Delete recordsets with same Date and Line - 05-18-2005 , 12:07 AM



That one really worked out!

Thank you so much. You are great!

Julia

--
Message posted via http://www.sqlmonster.com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.