OPの元の質問タイトル:サブクエリで1つのクエリのフィールドを使用する
私はAccessでクエリを作成した経験があまりなく、問題が発生しています。
私はいくつかのテーブルを持っています。1つは製品用、1つは市場用、1つは市場と製品を組み合わせたファクト用です。それらを結合できるクエリを作成する必要があり、特定の市場フィールド(MF)のすべてのインスタンスについて、特定のファクトフィールド(FF)のn番目に高いもの(市場/製品ごとに合計)を返す必要があります。リンク先のファクト値である特定の製品フィールド(PF)。それが理にかなっていることを願っています。
とにかく、これが私が持っているクエリです:
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
TOP 2の部分は、nを簡単に指定できる場所です。私が抱えている問題は、これをアクセスで実行すると、market.MF2の値を入力するように求められることです(これはサブクエリでそのインスタンスを参照していると思います)。私は、コードが各行のメインクエリからその値を取得すると考えていましたが、明らかにそれを実行していません。
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
クエリの何が問題になっていますか?見えない
少し早いですがお礼を
推測される結果:
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