0

mst_item と time_factor の 2 つのテーブルがあり、 mst_item には item、location、card の 3 つの列があり、 time_factor には location と card の 2 つの列があります

場所とカードの列を使用してこれら 2 つのテーブルを結合しようとしているとき。

要件は以下、

mst_item

item     location    card
xyz       R10        CRU
ABC       R10        LAT
CCC       R14        NAC

時間係数

location     card 
R10          CRU
R10          ALL
R14          ALL
R15          FX
R15          ALL

出力は、mast_item テーブルの location と card の両方が time_factor テーブルの location と card と一致する場合、一致するレコードを返す必要があります。

ex- R10 & CRU用

item     location    card
xyz       R10        CRU

場所のみが一致する場合は、time_factor テーブルからカードとして場所と「ALL」を返す必要があります。

いずれの場合も、一致するカードの値と「ALL」の両方を返すべきではありません。

ex- R14 および NAC 用

item     location    card
CCC       R14        ALL

クエリロジックを手伝ってください。

4

7 に答える 7

2

私はこれがトリックを行うべきだと思う...

select m.item, m.location, m.card from mst_card m, time_factor t
  where m.location = t.location and m.card = t.card
union all
select m.item, m.location, t.card from mst_card m, time_factor t
  where m.location = t.location and t.card = 'ALL'
    and not exists ( select 1 from time_factor t2 where t2.location=m.location and t2.card=m.card);
于 2013-11-07T13:19:33.453 に答える
0

要件の私の理解によれば、次のクエリが目的を果たします:

select item,mst.location,decode(mst.card,tym.card,tym.card,'ALL') from mst_item mst, time_factor tym where mst.location=tym.location;

于 2014-02-21T13:03:19.120 に答える
0

これはあなたが探しているものですか?

select test1.location,"card"= case 
          When test1.location = test2.location AND test1.card = test2.card then test2.card 
          When test1.location = test2.location AND test1.card != test2.card then 'ALL'
          End
from test1 inner join test2 on test1.location = test2.location

ここで、test1 は mst_item で、test2 は time_factor です。

ここにSQLフィドルのリンクがあります

http://sqlfiddle.com/#!3/9c6eb/8

于 2013-11-07T11:07:18.380 に答える
-1

これは、左外部結合を使用して解決できます

于 2015-12-25T08:44:52.790 に答える