Avatar billede micaud Mester
17. oktober 2008 - 22:31 Der er 7 kommentarer og
1 løsning

Sum og filtrering

Hej.

Jeg har følgende forespørgsel2 i Access som slår op i en Navision varetabel, og giver følgende resultat:

Varenr_            Bogføringsdato    Posttype    SumOfAntal
GU-GG 2965/S    31-jan-07      Køb                      12
GU-GG 2965/S    28-feb-07    Køb                      1
GU-GG 2965/S    12-mar-07    Køb                      3
GU-GG 2965/S    20-mar-07    Køb                      4
GU-GG 2965/S    31-mar-07    Køb                      1
GU-GG 2965/S    10-apr-07    Køb                      24
GU-GG 2965/S    30-apr-07    Køb                      16
GU-GG 2965/S    10-maj-07    Køb                      5
GU-GG 2965/S    31-maj-07    Køb                        58
GU-GG 2965/S    20-jun-07      Køb                      14

Herefter har jeg Forespørgsel1, som benytter Forespørgsel2 til at lave kolonnerne Akk og Akk1 samt tilføje Lager kolonnen fra en anden tabel.

Varenr_    Bogføringsdato    SumOfAntal    Akk    Akk1    Lager
GU-GG 2965/S    20-jun-07    14    14    -1    28
GU-GG 2965/S    31-maj-07    58    72    1    28
GU-GG 2965/S    10-maj-07    5    77    1    28
GU-GG 2965/S    30-apr-07    16    93    1    28
GU-GG 2965/S    10-apr-07    24    117    1    28
GU-GG 2965/S    31-mar-07    1    118    1    28
GU-GG 2965/S    20-mar-07    4    122    1    28
GU-GG 2965/S    12-mar-07    3    125    1    28
GU-GG 2965/S    28-feb-07    1    126    1    28
GU-GG 2965/S    31-jan-07    12    138    1    28

1. kolonne er varenr.
2. kolonne er dato for køb
3. kolonne er antal stk. købt
4. kolonne er en løbende sum af kolonne 3
5. kolonne er en formel der siger, hvis kolonne 4 minus kolonne 6(lager) mindre end 0 = -1 eller 1, hvis større end 0.

Jeg vil gerne have, at Access via en forespørgsel ender ud med at vise, at de 28 der ligger på lager er købt med 14 stk. den 20. juni og 14 stk. 31. maj, således jeg får en tabel med dato for køb af lagerbeholdningen med henblik på vurdering af alderen på lageret.

Men men men hvordan ?? Kan jeg eventuelt lave det hele i en forespørgsel, eller må jeg benytte hjæpeforespørgsler?

Bemærk, at tabellen i Access indeholder titusindevis af liner fordelt på 18.000 forskellige varenumre, hvor ovenstående kun indholder et varenummer.

Min SQL vedr. Forespørgsel1 af ovenstående ser således ud:

SELECT Forespørgsel2.Varenr_, Forespørgsel2.Bogføringsdato, Forespørgsel2.SumOfAntal, (SELECT SUM(SumOfAntal) FROM Forespørgsel2  AS T WHERE T.bogføringsdato >= Forespørgsel2.Bogføringsdato) AS Akk, IIf((SELECT SUM(SumOfAntal) FROM Forespørgsel2  AS T WHERE T.bogføringsdato >= Forespørgsel2.Bogføringsdato)-[lager]<0,-1,1) AS Akk1, Vare.Lager
FROM Forespørgsel2 INNER JOIN Vare ON Forespørgsel2.Varenr_ = Vare.Nummer
ORDER BY Forespørgsel2.Bogføringsdato DESC;

Håber virkelig I kan hjælpe, da jeg også har prøvet timevis via Excel.
Avatar billede terry Ekspert
18. oktober 2008 - 12:34 #1
Are you showing us all the information we would need?

Where do you see that the 28 in stock are purchased "med 14 stk. den 20. juni og 14 stk. 31. maj"?
Avatar billede micaud Mester
18. oktober 2008 - 14:26 #2
The table shows all buyings divided by date so last buying was on June 20, 2007 with 14 stk. and secound last buying was on May 31, 2007 with 58 stk. so if I have 28 on stock 14 must be from June 20, 2007 and the last 14 from May 31, 2007. The princip is First In First Out so the last buying is the one in stock.
Avatar billede terry Ekspert
18. oktober 2008 - 14:53 #3
Can you explain in more detail please? First I dont see what you 28 in stock has to do with your running total (Akk).

You have 28 in stock for all dates, how can that be if ther are a number of "køb"?
If you sell something your stock decreases, and if you buy then it increases.

Who purchases "køb"? Your company or a customer?

If this query shows "køb" for specific dates
Varenr_            Bogføringsdato    Posttype    SumOfAntal
GU-GG 2965/S    31-jan-07    Køb                      12
GU-GG 2965/S    28-feb-07    Køb                      1
GU-GG 2965/S    12-mar-07    Køb                      3
GU-GG 2965/S    20-mar-07    Køb                      4
GU-GG 2965/S    31-mar-07    Køb                      1
GU-GG 2965/S    10-apr-07    Køb                      24
GU-GG 2965/S    30-apr-07    Køb                      16
GU-GG 2965/S    10-maj-07    Køb                      5
GU-GG 2965/S    31-maj-07    Køb                      58
GU-GG 2965/S    20-jun-07    Køb                      14

Then on 31-maj-07 58 were purchased, or at least as far as I can see.


You will ned to explain in detail and with examples so we can understand what your after.
Avatar billede micaud Mester
18. oktober 2008 - 17:11 #4
The question for me is "How old is my stock?" In one table I have my total stock by pieces and in another table I have all entries "sold, bought, downwriting etc." So if you sum all entries for one item it would be equal to the total stock. The item GU-GG 2965/S has a stock of 28 today. The query a bow shows all buying of that item which the company have bought. In total we have bought 138 (sum of the query) It dos not matter how many we have sold because I know we have 28 in stock out of the total buy of 138 pieces. I also know that I use the First In First Out princip.

I just thought that I need a running total to help me but perhaps that is not necessery?.

I was thinking of using Akk1 in the query "Forespørgsel1" so when is <0 then return SumOfAntal but it dos not work in line two, it will only give me the first line with 14 pieces so I miss the last 14 bought in May 07 !!!!

I would like Access to give me this output:

Varenr_            Bogføringsdato    Posttype    SumOfAntal
GU-GG 2965/S    31-maj-07    Køb                      14
GU-GG 2965/S    20-jun-07    Køb                      14

Then I know my stock of that item is from May and June 2007

If I instead have 80 in stock the output should be:

Varenr_            Bogføringsdato    Posttype    SumOfAntal
GU-GG 2965/S    30-apr-07    Køb                      3
GU-GG 2965/S    10-maj-07    Køb                      5
GU-GG 2965/S    31-maj-07    Køb                      58
GU-GG 2965/S    20-jun-07    Køb                      14

Therefore I need a table of the buying to see when the last items are bought.

In another way "when is my 28 in stock bought out of the 138 pieces I have bought total of that item?"

So I am stock and need help :-)

Please ask more if you think you can help as I really would like to solve it.
Avatar billede terry Ekspert
18. oktober 2008 - 17:40 #5
Ok, I think I understand what your after and I dont think you can do this in a query.
But I would need to play around with your queries a bit to be sure.

Is it possibe for you to send an Access table with some test data (GU-GG 2965/S for example) an dthe two queries you are using so I can reproduce the examples you show?

I'm off out for the evening but I should have a bit of time tommorrow.

ekspertenATsanthell.dk
AT = @
Avatar billede micaud Mester
18. oktober 2008 - 17:51 #6
I will make a small test database and send you. Perhaps this evening or maybe on Monday..
Avatar billede terry Ekspert
19. oktober 2008 - 09:20 #7
Thats OK, hope your not in a rush.

Would using a TEMP table to contain the result be an option? If so I think this would be the easiest solution.
Avatar billede micaud Mester
20. oktober 2008 - 13:06 #8
Hi Terry.

I have made my Access database and in Excel made the correct filter etc. so I have a solution now.

Perhaps it could be done more easialy in Access - I do not know.

Thank you for your interest and participation.
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



IT-JOB