17. december 2002 - 20:19Der er
18 kommentarer og 1 løsning
Kriterier i Query
Jeg har to tabeller jeg tager fra. Kravet er at ID fra den ene skal passe med et ID felt i den anden tabel. Det gør den også fint, men hvordan sætter man det op, så den kun tager den nyeste record med identisk ID? Hvis der ikke er noget der passer til, skriver den feltet i Query'en med det felt tom. Det gør jeg med or Is Null
Hvad skal man skrive for den gemmer kun den nyeste record til et bestemt ID. Alle records til andre ID skal selvfølgelig blive i Query'en. Kan dette overhovedet lade sig gøre.
If you make a query with the two tables then if they are related and you have set the primary and foreign keys up correctly you should see a thin line joining the primary key from one table with the foreign key in the other table. If you now right click on this thin line you should have three possibilities, choose the one which suits your needs. Now when you run the query you will also see records on the primary key side even when the foreign key side is empty.
make a query. If there is NOT a thin line between ID and BillederID then drag ID (with the mouse) over to BilledID. You should now have a thin line (JOIN) between the two tables.
Now in the sort field for the ID field choose DESC (Descending)
Now right click in the are where the tables are and choose properties from the menu. In the properties you will see a field (Top Values) it will be = All change this to 1. This is the same as TOP 1 for the newest ID
Not excatly that. It have to be top 1 from each ID. In table two you can write more than one record to combines with ID i table one. So all ID's fra table one and top 1 from table two that ID equals BillederID
SELECT tblOne.ID, tblOne.Overskrift, tblOne.URL, tblOne.Dato, First(tblTwo.Kommentar) AS TopKommentar FROM tblOne LEFT JOIN tblTwo ON tblOne.ID = tblTwo.BilledID GROUP BY tblOne.ID, tblOne.Overskrift, tblOne.URL, tblOne.Dato;
This only gives you the first of each comment, if you want the TOP then you will have to sort the kommentar field in which ever order you want but I cant see what use that is.
I have NO idea what the fields contain so I cant say. If you run the query in Access query designer you will see EXACTLY what it is finding. Then you have some ide as to what is wrong. IF there is only one record in Billeder and zero, one or more records in Billederindlaeg then the query SHOULD give you what you want, but as I said I cant see what you have!
Tilføj en forespørgsel med alle de mulige ID-numre og klik på Vis/Totaler og vælg Maks. Hvis nyeste ID-nummer altid ligger sidst i tabellen kan du også vælge Sidste.
better place the solution here, and thanks mrs :o)
A query (qryCommnt) for selecting the comments in the correct order. and as Kommentar is a memo field I have to select only the first 255 characters (text field).
SELECT Billederindlaeg.ID, Billederindlaeg.BillederID, Left([Kommentar],255) AS comment FROM Billederindlaeg ORDER BY Billederindlaeg.ID DESC;
and now the query putting it all together.
SELECT Billeder.ID, Billeder.Overskrift, Billeder.Url, Billeder.Dato, (SELECT TOP 1, comment FROM qryComment WHERE qryComment.BillederID = [Billeder]![ID]) AS Comment FROM Billeder;
I'm sure it could be put into one SQL but I find this easier.
Synes godt om
Ny brugerNybegynder
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.