0

I have two tables in an Access database, tblProducts and tblProductGroups.

I am trying to run a query that joins both of these tables, and brings back a single record for each product. The problem is that the current design allows for a product to be listed in the tblProductGroups table more than 1 - i.e. a product can be a member of more than one group (i didnt design this!)

The query is this:

select tblProducts.intID, tblProducts.strTitle, tblProductGroups.intGroup 
from tblProducts 
inner join tblProductGroups on tblProducts.intID = tblProductGroups.intProduct 
where tblProductGroups.intGroup = 56 
and tblProducts.blnActive 
order by tblProducts.intSort asc, tblProducts.curPrice asc

At the moment this returns results such as:

intID | strTitle | intGroup
1     | Product 1 | 1
1     | Product 1 | 2
2     | Product 2 | 1
2     | Product 2 | 2

Whereas I only want the join to be based on the first matching record, so that would return:

intID | strTitle | intGroup
1     | Product 1 | 1
2     | Product 2 | 1

Is this possible in Access?

Thanks in advance Al

4 Answers 4

4

This option runs a subquery to find the minimum intGoup for each tblProducts.intID.

SELECT tblProducts.intID
, tblProducts.strTitle
, (SELECT TOP 1 intGroup 
   FROM tblProductGroups 
   WHERE intProduct=tblProducts.intID 
   ORDER BY intGroup ASC) AS intGroup
FROM tblProducts 
WHERE tblProducts.blnActive 
ORDER BY tblProducts.intSort ASC, tblProducts.curPrice ASC
3
  • Hi - thanks for your response. Thats not quite what I'm looking for though. I'm looking for it to return multiple rows, but only based on the first match with the tblProductGroups table.
    – higgsy
    May 25, 2011 at 17:26
  • 1
    Ummm.... That's what this query should do: one row for each Product, with its "first" (lowest-id) Product Group. May 25, 2011 at 18:42
  • Hi - I tried your example (perhaps after it was edited) and it worked absolutely perfectly - thank you very much, just what I needed.
    – higgsy
    May 27, 2011 at 7:52
2

This works for me. Maybe this helps someone:

SELECT 
    a.Lagerort_ID,
    FIRST(a.Regal) AS frstRegal,
    FIRST(a.Fachboden) AS frstFachboden, 
    FIRST(a.xOffset) AS frstxOffset, 
    FIRST(a.yOffset) AS frstyOffset, 
    FIRST(a.xSize) AS frstxSize, 
    FIRST(a.ySize) AS frstySize, 
    FIRST(a.Platzgr) AS frstyPlatzgr, 
    FIRST(b.Artikel_ID) AS frstArtikel_ID, 
    FIRST(b.Menge) AS frstMenge, 
    FIRST(c.Breite) AS frstBreite, 
    FIRST(c.Tiefe) AS frstTiefe, 
    FIRST(a.Fachboden_ID) AS frstFachboden_ID, 
    FIRST(b.BewegungsDatum) AS frstBewegungsDatum, 
    FIRST(b.ErzeugungsDatum) AS frstErzeugungsDatum
FROM ((Lagerort AS a) 
LEFT JOIN LO_zu_ART AS b ON a.Lagerort_ID = b.Lagerort_ID)
LEFT JOIN Regal AS c ON a.Regal = c.Regal
GROUP BY a.Lagerort_ID
ORDER BY FIRST(a.Regal), FIRST(a.Fachboden), FIRST(a.xOffset), FIRST(a.yOffset);

I have non unique entries for Lagerort_ID on the table LO_zu_ART. My goal was to only use the first found entry from LO_zu_ART to match into Lagerort.

The trick is to use FIRST() an any column but the grouped one. This may also work with MIN() or MAX(), but I have not tested it.

Also make sure to call the Fields with the "AS" statement different than the original field. I used frstFIELDNAME. This is important, otherwise I got errors.

1

Create a new query, qryFirstGroupPerProduct:

SELECT intProduct, Min(intGroup) AS lowest_group
FROM tblProductGroups 
GROUP BY intProduct;

Then JOIN qryFirstGroupPerProduct (instead of tblProductsGroups) to tblProducts.

Or you could do it as a subquery instead of a separate saved query, if you prefer.

0

It's not very optimal, but if you're bringing in a few thousand records this will work:

  1. Create a query that gets the max of tblProducts.intID from one table and call it qry_Temp.
  2. Create another query and join qry_temp to the table you are trying to join against, and you should get your results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.