04. februar 2008 - 09:51Der er
20 kommentarer og 2 løsninger
Autoincrement i forespørgsel
Hej alle
Jeg forsøger at lave en forespørgsel hvor FELT2 er er autoincrement felt der skifter for hver ny linie. FELT2 skal dog starte forfra for hver ændring i FELT1 også. Se eksempel:
you cant create an autoincrement field in a select query but you could maybe do it with a calculated field. This will require that you have for example an autonumber column (unique).
SELECT T.ID, T.FELT1, (SELECT count(*) FROM Table1 T2 WHERE T2.FELT1 = T.FELT1 AND T2.ID < T.ID)+1 AS FELT2 FROM Table1 AS T;
Synes godt om
Slettet bruger
04. februar 2008 - 10:32#4
Jeg har lavet en funktion jeg kan bruge i min forespørgsel:
Global IncVar As Long Function IncrementValues(i) As Long IncVar = IncVar + 1 IncrementValues = IncVar End Function
Men som sagt opstår der problemer fordi jeg ønsker den starter forfra ved skift i FELT1
Synes godt om
Slettet bruger
04. februar 2008 - 10:35#5
Hvis du tilføjer FELT2 i din tabel, så kan du bruge denne funktion:
Dim db As DAO.Database Dim rst As DAO.Recordset Dim a, b, c Set db = CurrentDb() Set rst = db.OpenRecordset("DinTabel") With rst .MoveFirst a = 1 b = !felt1 Do c = !felt1 If b = c Then .Edit !felt2 = a .Update a = a + 1 Else .Edit !felt2 = 1 .Update b = !felt1 a = 2 End If .MoveNext Loop Until .EOF .Close End With
Synes godt om
Slettet bruger
04. februar 2008 - 10:40#6
Mugs (og andre) hvis i skal have hele historien, så kommer den her. Jeg har en tabel der indeholder følgende information:
Det jeg ønsker at finde ud af er for hvert ID, hvor lang tid der er gået mellem START og SLUT. Der kan være mange START og SLUT for hvert ID, men der skal altid være et SLUT for hvert START.
Jeg forsøgte mig med 2 forespørgsler, 1 der grupperer START og 1 der grupperer SLUT, for derefter at linke dem sammen på ID feltet og et efterfølgende autonumerings felt. Det går pt. ikke så godt hehe, så nu får i hele ihistorien i håb om at i kender en bedre løsning.
Der er intet link mellem START og SLUT linierne, og det er ikke muligt at ændre tabellerne til at omfatte det, det er altsammen data jeg henter fra en backend server.
I'm finding it difficult to see what the original question has to do with your comment 04/02-2008 10:40:05
Synes godt om
Slettet bruger
04. februar 2008 - 11:54#8
Mugs spurgte hvad formålet med den oprindelige forespørgsel var, og jeg skrev hele problemstillingen.
Jeg har forsøgt at løse problemstillingen ved hjælp af autoincrement, men hvis der er andre løsninger tager jeg dem gerne i betragtning, da autoincrement måske er lidt ustabilt set i bakspejlet.
The answer I gave 04/02-2008 10:13:47 can be modified to give you a calculated 'autoincrermnet' as in the example but what you are realy after is the time difference between each start/slut for a specific ID isnt it?
This is using the fields which you have and the result is in seconds
SELECT T1.ID, T1.AKTIVITET, T1.DATO, T1.KLOKKESLET, (SELECT Max(DATO) FROM tabel1 T2 WHERE T2.ID = T1.ID AND ((T2.DATO < T1.DATO AND T2.KlOKKESLET >= T1.KLOKKESLET) OR (T2.DATO = T1.DATO AND T2.KlOKKESLET < T1.KLOKKESLET))) AS LASTDATO, (SELECT Max(KLOKKESLET) FROM tabel1 T2 WHERE T2.ID = T1.ID AND ((T2.DATO < T1.DATO AND T2.KlOKKESLET >= T1.KLOKKESLET) OR (T2.DATO = T1.DATO AND T2.KlOKKESLET < T1.KLOKKESLET))) AS LASTKLOKKESLET, CDate([LASTDATO] & " " & [LASTKLOKKESLET]) AS LASTTID, DateDiff("s",CDate([LASTDATO] & " " & [LASTKLOKKESLET]),CDate([DATO] & " " & [KLOKKESLET])) AS DiffSec FROM Tabel1 AS T1;
Synes godt om
Slettet bruger
05. februar 2008 - 13:27#14
Hi Terry
If you instead of using DATO and KLOKKESLET use MODIFY_DATETIME (which is time in seconds since 1/1/1990 00:00:00), then th equery should get much simpler right? :)
Synes godt om
Slettet bruger
05. februar 2008 - 13:31#15
But also Terry, your query is assuming there is only two occurrences os START and SLUT, but there can be many, I've IDs that have over 20 START and SLUT rows :-)
05/02-2008 13:27:44 Access doesnt understand MODIFY_DATETIME but if you had the data and time in one field then that would make things more simpler.
05/02-2008 13:31:26 There can be as many STARTs and STOPs as you like for each ID. The query calculates the differnece in seconds between the previous AKTIVITET which means that you can also see the difference between a STOP and the next START.
The first record for each ID will give an error because there is no previous record.
Have you tried it with your data? I used the example you gave and was assuming that it was what you were using.
I get that Access doesn't understand MODIFY_DATETIME as a valid DATE field, but it's a perfectly valid numeric field which should be usable in your query aswell.
I tried your query, but modified it a bit to use MODIFY_DATETIME instead, but the result was a bit weird :-)
Synes godt om
Slettet bruger
05. februar 2008 - 15:48#18
Also, I just want the query to group each instance of START and SLUT, so the end result looks a bit like this:
Well if you put a number in the the fields instead of a dat then its just a case of subtracting them form each other.
When you say "group each instance..." do you mean sort?
is it possible for you to send an example of teh data you are using so that I can see exactly what table/field names you are using and so that I have some data which I can test.
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.