Avatar billede puppetmaster Nybegynder
22. juni 2005 - 15:39 Der er 21 kommentarer og
1 løsning

Kun 2 poster for hvert medarbejdernummer

Tabellen indeholder timetal for medarbejdere, for 14 dages perioder, e.g.

Dato    MedarbNr Timetal
*************************
01-05-05  4000      34
01-05-05  5000      36
15-05-05  5000      37
15-05-05  6000      40
29-05-05  4000      40
29-05-05  5000      37
29-05-05  6000      41

Jeg vil gerne have timetallet for medarbejderne, for 2 perioder bagud. Hvis en medarbejder f.eks. har holdt 2 uges ferie (dækkende hele perioden), skal den periode ikke tages med men den foregående periode. Ovenstående skulle gerne resultere i at jeg får timetallet for medarbejder 4000 for datoerne 01-05-05 og 29-05-05, for 5000 for datoerne 01-05-05 og 15-05-05 samt for 6000 datoerne 15-05-05 og 29-05-05
Avatar billede terry Ekspert
22. juni 2005 - 16:22 #1
Are you sayin that it is the two records with the lowest dates? How do we know when a medarb has been on holiday?
Can you explain a bit more please?
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 09:25 #2
Sure can, Terry.
Not with the "lowest" dates, but the newest dates.
the dates are always sunday, in a 2 week period.
Sunday 2. of January, sunday 16. of January, sunday 30. of January and so on.
medarbnr means employee no. (guess you know that)
The only way we can see that an employee has been on holiday, is that there are NO records on a particular date. The date represents a payment day.
I presented a demo for my boss (it's his assignment, but I offered to help) in which I use a pivottable, but since I was unable to built-in the above logic into the query, I chose to present data for 6 periods, which isn't what he wanted. Just the 2 newest periods pr. employee.
Avatar billede terry Ekspert
23. juni 2005 - 13:22 #3
Just need to check something. (Not with the "lowest" dates, but the newest dates.)!!!

Two newsest dates for 5000 are 15-05-05 and 29-05-05, but you say in th eoriginal question (for 5000 for datoerne 01-05-05 og 15-05-05) which are the two oldest (lowest)!!!
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 13:53 #4
You are correct, terry, my mistake, for 5000 I want 15-05-05 and 29-05-05.
Avatar billede terry Ekspert
23. juni 2005 - 13:56 #5
Hope this gives you waht your after

SELECT T1.MedarbNr, T1.dato, T1.TimeTal
FROM tbltimetal AS T1
WHERE ((((Select Count (*) from tbltimetal Where [dato] > [T1].[dato] AND MedarbNr = t1.MedarbNr)+1) In (1,2)))
ORDER BY T1.MedarbNr, T1.dato;
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 14:20 #6
I'm not quite sure what the above should result in....doesn't the
tbltimetal AS T1 cause an error?
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 14:22 #7
Nope it doesn't! I just had to rename some of the fields! Thanks a LOT, terry!
Avatar billede terry Ekspert
23. juni 2005 - 14:23 #8
selv thanks :o)
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 15:47 #9
Could you tell me where you know the IN criteria from?! :)
Avatar billede terry Ekspert
23. juni 2005 - 15:49 #10
thats my secret :o)
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 15:50 #11
Aaaaw, come on! :)
Avatar billede terry Ekspert
23. juni 2005 - 15:52 #12
Just joking!

Not sure if you understand the SQL but what the SUB Select does is makes a new column to sort the records, and because I only want the newest 2 then it must be IN(1,2)
if I wanted the first 3 the just change it to IN(1,2,3)

:o)
Avatar billede terry Ekspert
23. juni 2005 - 15:53 #13
but I cant go telling you all my secrets otherwise you would be as good as me :o)
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 15:54 #14
Yeah, I'm "with it", just never saw the IN(1,2) before.
Thanks again, for everything.
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 15:54 #15
:)
Avatar billede terry Ekspert
23. juni 2005 - 15:56 #16
oh! In(1,2) is the same as WHERE 1 or 2
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 15:59 #17
Can you elaborate the SQL a bit?
I also have a field, Pay, for every record in the table and I would like the SQL to calculate an average kr/hour
Dato    MedArbNr  Timetal  Pay
01-05-05  4000      34      5000
15-05-05  4000      36      5200

Total of Pay  divided by Total of Timetal
(5000+5200) /(34+36)
Avatar billede puppetmaster Nybegynder
23. juni 2005 - 15:59 #18
aaaah, I see!
Avatar billede puppetmaster Nybegynder
24. juni 2005 - 14:35 #19
terry, is it impossible to do?
Avatar billede terry Ekspert
27. juni 2005 - 08:30 #20
sorry I've been away for the weekend

"aaaah, I see!" does this meen you have a solution?
Avatar billede puppetmaster Nybegynder
27. juni 2005 - 08:46 #21
Well, not quite, I still need it to calculate this:


I also have a field, Pay, for every record in the table and I would like the SQL to calculate an average kr/hour
Dato    MedArbNr  Timetal  Pay
01-05-05  4000      34      5000
15-05-05  4000      36      5200

Total of Pay  divided by Total of Timetal
(5000+5200) /(34+36)
Avatar billede terry Ekspert
27. juni 2005 - 11:46 #22
the easiest way is to make a query which takes the data from the first query

SELECT qry.MedarbNr, Sum(Pay)  /  Sum(TimeTal) AS Tot
FROM qry
GROUP BY qry.MedarbNr;
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