Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8951141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:35:39+00:00 2026-06-15T13:35:39+00:00

OP’s original question title: Use a field from one query in a subquery I

  • 0

OP’s original question title: Use a field from one query in a subquery

I am not all that experienced in writing queries in Access and I’m having some trouble.

I have a few tables. 1 for Products, 1 for Markets and 1 for Facts which combine the Markets and Products. I need to write a query that can join those together and for every instance of a specific market field (MF), I need to return the nth highest of a specific fact field (FF) (summed up per market/product) and which of a specific product field (PF) that fact value it is links to. Hope that makes sense.

Anyway, here is the query I have:

select market.MF2, product.PF10, sum(fact.FF3) as FF3
from mMarket market, mProduct product, mFact fact
where market.Item_ID = fact.Market_ID
and product.Item_ID = fact.Product_ID
and FF3 = 
(
  select min(FF3) from 
  (
    select TOP 2 FF3 from 
    (
      select market2.MF2, product2.PF10, sum(fact2.FF3) as FF3
      from mMarket market2, mProduct product2, mFact fact2
      where market2.Item_ID = fact2.Market_ID
      and product2.Item_ID = fact2.Product_ID
      and market2.MF2 = market.MF2
      group by market2.MF2, product2.PF10
      order by 3 DESC
    )
  )
)
group by market.MF2, product.PF10

The TOP 2 part is where to specify n easily.
The problem I am having is that when I run this in access, It prompts me to enter a value for market.MF2 (I presume this is referring to the instance of it in the subquery).
I was thinking that the code would grab that value from main query for each row but clearly its not doing that.

Tables below:

mMarket

Item_ID     MF2
---------------
1           64
2           28
3           73

mProduct

Item_ID     PF5        PF10
----------------------------
1           2221       Category1
2           6487       Category3
3           73234      Category2
4           76223      Category1
5           99342      Category2

mFact

Market_ID        Product_ID       FF3
--------------------------------------
1                1                1000
1                2                1500
1                3                500
1                4                1000
2                1                1500
2                3                1000
2                5                1500
3                1                1000
3                2                500
3                5                2000

What is wrong with the query? I can’t see it

Thank in advance

Expected Results:

If n was 1

MF2        PF10         FF3
----------------------------
64         Category1    2000
28         Category2    2500
73         Category2    2000

If n was 2

MF2        PF10         FF3
----------------------------
64         Category3    1500
28         Category1    1500
73         Category1    1000
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:35:40+00:00Added an answer on June 15, 2026 at 1:35 pm

    Take a look at this reference please:

    • SQLFIDDLE

    Query Sum just by Market:

    Results Sum just by Market:

    MF2 ITEM_ID PRODUCT_ID  PF5     PF10                                        SUM(F.FF3)
    28  2       5,1,3       99342   Category2,Category1,Category2               4000
    64  1       3,4,1,2     73234   Category2,Category1,Category1,Category3     4000
    73  3       1,2,5       2221    Category1,Category3,Category2               3500
    

    Query Sum by Market, Category :

    select m.mf2, p.pf10, p.pf5, 
    group_concat(f.product_id)as prods, 
    m.item_id , sum(f.ff3) as sff
    from mmarket m
    left join mfact f
    on m.item_id = f.market_id
    inner join mproduct p
    on f.product_id = p.item_id
    group by m.mf2, p.pf10
    order by sff desc 
    ;
    

    Results Sum by Market, Category :

    MF2     PF10        PF5     PRODS   ITEM_ID     SFF
    28      Category2   99342   5,3     2           2500
    64      Category1   76223   4,1     1           2000
    73      Category2   99342   5       3           2000
    28      Category1   2221    1       2           1500
    64      Category3   6487    2       1           1500
    73      Category1   2221    1       3           1000
    64      Category2   73234   3       1           500
    73      Category3   6487    2       3           500
    

    UPDATED ANSWER as per OP’s later comments

    Query:

    select x.*
    from (
    select m.mf2, p.pf10, sum(f.ff3) as sff
    from mmarket m
    left join mfact f
    on m.item_id = f.market_id
    inner join mproduct p
    on f.product_id = p.item_id
    group by m.mf2, p.pf10
      order by sff desc ) as x
    limit 1
    ;
    

    Results:

    MF2     PF10          MSFF
    28      Category2     2500
    

    Based on OP’s expected results – updated to comply with MS ACCESS SQL

    There are many ways to achieve this in MYSQL. However wanted to give OP the answer for MS ACCESS.

    I suggest you save the above Results from ** Sum by Market, Category** query into a temp table or to a MS ACCESS Query. Then use that Query in your final Query.

    • SQLFIDDLE

    Query for Top 1st by Market by Category:

    -- success final :) by Top 1st
    select x.mf2, x.pf10, x.sff
    from 
    (select m.mf2, p.pf10, p.pf5, 
    group_concat(f.product_id)as prods, 
    m.item_id , sum(f.ff3) as sff
    from mmarket m
    left join mfact f
    on m.item_id = f.market_id
    inner join mproduct p
    on f.product_id = p.item_id
    group by m.mf2, p.pf10
    order by sff desc) as x
    
    where 
    
    (select count(*)
     from
         (select m.mf2, p.pf10, p.pf5, 
          group_concat(f.product_id)as prods, 
          m.item_id , sum(f.ff3) as sff
          from mmarket m
          left join mfact f
          on m.item_id = f.market_id
          inner join mproduct p
          on f.product_id = p.item_id
          group by m.mf2, p.pf10
          order by sff desc) as y
    
    where y.sff >= x.sff
    and y.mf2 = x.mf2) =1 //-- Top 3rd, 2nd, 1st
    order by x.sff desc
    ;
    

    Results for Top 1st Market by Category:

    MF2     PF10    SFF
    28  Category2   2500
    64  Category1   2000
    73  Category2   2000
    

    Results for Top 2nd Market by Category:

    MF2     PF10    SFF
    28  Category1   1500
    64  Category3   1500
    73  Category1   1000
    

    Results for Top 3rd Market by Category:

    MF2     PF10    SFF
    64  Category2   500
    73  Category3   500
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y’all
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.