Avatar billede redbulldk Juniormester
31. marts 2017 - 17:35 Der er 9 kommentarer og
3 løsninger

Access fjerne dubletter men behold den ældste

Hej

Jeg har en udfordring med dubletter i en Access database, hvor jeg ønsker at slette dubletterne, men beholde den ældste post.

Eksempel i nedenstående, har jeg kolonerne "Nr", "bruger" og "Dato", hvor jeg udelukkende ønsker de ældste kombinationen af ("Nr" + "bruger"), som er markeret med "<--"

Nr | bruger | Dato
100 | NN | 2017-03-27
100 | NN | 2017-03-30
100 | NN | 2017-03-25 <--
100 | TT | 2017-03-26 <--
200 | TT | 2017-03-30
200 | NN | 2017-03-25 <--
200 | NN | 2017-03-27
200 | TT | 2017-03-30 <--

Efter fjernelse af dubletter, skal listen kun indeholde følgende.
100 | NN | 2017-03-25 <--
100 | TT | 2017-03-26 <--
200 | NN | 2017-03-25 <--
200 | TT | 2017-03-30 <--

Hvordan gør jeg det?
Avatar billede terry Ekspert
31. marts 2017 - 17:55 #1
Avatar billede redbulldk Juniormester
31. marts 2017 - 21:46 #2
Hi Terry,

Thanks for your prompt reply. It works - Great :)

Just a supplementary question. I am new beginning in SQL. There are several columns. If for example I would like to have content from ColD + ColE list in the view, how SQL commands then be?

SELECT Min (tblTest.ColA) cola, tblTest.ColB, tblTest.ColC
FROM tblTest
GROUP BY tblTest.ColB, tblTest.ColC
Avatar billede terry Ekspert
02. april 2017 - 09:37 #3
Not exactly sure what your after, could you show some data and also the result you expect?
Avatar billede terry Ekspert
02. april 2017 - 09:43 #4
Oh, and please mark my answer as correct, if it works of course ;-)
Avatar billede redbulldk Juniormester
02. april 2017 - 14:56 #5
Hi Terry,

Thanks again.

I added example of data I want to.

No | User | Date | City | Note
100 | NN | 2017-03-27 | Randers | På Citytur
100 | NN | 2017-03-30 | Glostrup | En is tur
100 | NN | 2017-03-25 | Hillerød | Skovtur <--
100 | TT | 2017-03-26 | Odense | I ZOO <--
200 | TT | 2017-03-30 | Padborg | Over grænsen
200 | NN | 2017-03-25 | Flensborg | Shopping <--
200 | NN | 2017-03-27 | Rønne | Ud at sejle
200 | TT | 2017-03-30 | København | Arbejde <--

The result I want is the following:
No | User | Date | City | Note
100 | NN | 2017-03-25 | Hillerød | Skovtur <--
100 | TT | 2017-03-26 | Odense | I ZOO <--
200 | NN | 2017-03-25 | Flensborg | Shopping <--
200 | TT | 2017-03-30 | København | Arbejde <--

The following works,
SELECT Min (tblTest.Date) AS Date, tblTest.No, tblTest.User
FROM tblTest
GROUP CITY tblTest.No, tblTest.User

But if I try to add the col. Like "City" and "Note" like the below:

SELECT Min (tblTest.Date) AS Date, tblTest.No, tblTest.User, tblTest.City, tblTest.Note
FROM tblTest
GROUP CITY tblTest.No, tblTest.User

I get a Access promp with following message:

"Din forespørgsel indeholder ikke det angivne udtryk "City" som en del af en aggregatfuntion"

What do I wrong?
Avatar billede terry Ekspert
02. april 2017 - 18:17 #6
In the link I gave we delete the records we dont want
https://www.computerworld.dk/eksperten/spm/1015214

So the table only contains the records you want in result IE

No | User | Date | City | Note
100 | NN | 2017-03-25 | Hillerød | Skovtur <--
100 | TT | 2017-03-26 | Odense | I ZOO <--
200 | NN | 2017-03-25 | Flensborg | Shopping <--
200 | TT | 2017-03-30 | København | Arbejde <--

Are you using the second SQL to delete them?

You asked for "fjerne dubletter men behold den ældste"!
Avatar billede terry Ekspert
02. april 2017 - 18:18 #7
Are you using the second SQL to delete those you dont want?
Avatar billede redbulldk Juniormester
02. april 2017 - 18:29 #8
No, I use the query only for Excel, where I make data connection between Excel and Access.
Avatar billede terry Ekspert
02. april 2017 - 19:23 #9
So your original questions didn't include delete of records?

OK, I think I can find a solution but first tomorrow as I'm a bit busy right now....
Avatar billede terry Ekspert
03. april 2017 - 20:02 #10
Not sure that your query works

"The following works,
SELECT Min (tblTest.Date) AS Date, tblTest.No, tblTest.User
FROM tblTest
GROUP CITY tblTest.No, tblTest.User"

But this does. I have changed name of field Date to Dato. Date is a reserved word ;.)

Any, make a query using this SQl and name it qry1

SELECT tblTest.[No], tblTest.User, Min(tblTest.Dato) AS MinOfDato
FROM tblTest
GROUP BY tblTest.[No], tblTest.User;


Then make another query
SELECT tblTest.ID, tblTest.[No], tblTest.User, tblTest.Dato, tblTest.City, tblTest.Note, *
FROM tblTest INNER JOIN qry1 ON (tblTest.Dato = qry1.MinOfDato) AND (tblTest.User = qry1.User) AND (tblTest.[No] = qry1.[No]);
Avatar billede redbulldk Juniormester
04. april 2017 - 21:57 #11
Thanks Terry,

It works now :)
Avatar billede terry Ekspert
05. april 2017 - 10:01 #12
Super !
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester