0

我有 2 个表 mst_item 和 time_factor , mst_item 有 3 列 item,location,card 和 time_factor 有 2 列 location 和 card

当我尝试使用位置和卡片列加入这两个表时。

要求如下,

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 匹配,那么它应该返回匹配的记录,

用于 R10 和 CRU

item     location    card
xyz       R10        CRU

如果只有位置匹配,那么它应该从 time_factor 表中返回位置和“ALL”作为卡片。

在任何情况下,它都不应该返回匹配的卡值和“ALL”。

前 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

要在仅位置匹配时返回 ALL,请尝试以下查询

SQL查询

select m.item, m.location,
(CASE WHEN
m.card like t.card THEN m.card
ELSE
'All'
END) as card 
from 
mst_item as m,
time_factor as t 
WHERE
m.location like t.location

甲骨文查询

select mst_item.item, mst_item.location,
(CASE 
 WHEN
 mst_item.card = time_factor.card THEN mst_item.card
 ELSE
 'All'
 END) as card 
 from 
 mst_item,
 time_factor  
 WHERE
 mst_item.location like time_factor.location;

输出

在此处输入图像描述

更好的查询可以是

SQL查询

select m.item, m.location,t.card
from 
mst_item as m,
time_factor as t 
WHERE
m.location like t.location

甲骨文查询

select mst_item.item, mst_item.location, time_factor.card
from 
mst_item,
time_factor  
WHERE
mst_item.location like time_factor.location;

输出

在此处输入图像描述

另一种变体可以是

select m.item, m.location,
(CASE WHEN
m.card like t.card THEN m.card
WHEN
t.card like 'ALL' THEN t.card
END) as mcard 
from 
mst_item as m,
time_factor as t 
WHERE
m.location like t.location 
AND (CASE WHEN
m.card like t.card THEN m.card
WHEN
t.card like 'ALL' THEN t.card
END) != '';

一个变种可以是

SQL查询

select m.item, m.location,
(CASE WHEN
m.card like t.card THEN m.card
WHEN
t.card like 'ALL' THEN t.card
END) as mcard 
from 
mst_item as m,
time_factor as t 
WHERE
m.location like t.location 
AND (CASE WHEN
m.card like t.card THEN m.card
WHEN
t.card like 'ALL' THEN t.card
END) != '';

甲骨文查询

select mst_item.item, mst_item.location,
(CASE 
WHEN
mst_item.card = time_factor.card THEN mst_item.card
WHEN
time_factor.card like 'ALL' THEN time_factor.card 
END) as card 
from 
mst_item,
time_factor  
WHERE
mst_item.location like time_factor.location
AND
(CASE 
WHEN
mst_item.card = time_factor.card THEN mst_item.card
WHEN
time_factor.card like 'ALL' THEN time_factor.card 
END) IS NOT NULL
;

输出

在此处输入图像描述

请检查您想要的输出,以便我们进行相应的修改。

在http://sqlfiddle.com/#!2/53e53/21上的 SQL 小提琴演示

Oracle 小提琴演示在http://sqlfiddle.com/#!4/d28b1/26

于 2013-11-07T10:04:05.583 回答
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 Fiddle 链接

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

于 2013-11-07T11:07:18.380 回答
0

我认为下面的查询会有所帮助。

SELECT DISTINCT
mi.item,
mi.location,
CASE
WHEN Tf_Query.location=mi.location AND Tf_Query.card=mi.card
THEN Tf_Query.card
WHEN Tf_Query.location=mi.location AND Tf_Query.card!=mi.card
THEN mi.card
END card
FROM
mst_item mi,
(SELECT
tf.location LOCATION,max(tf.card) card
FROM
time_factor tf
GROUP BY (LOCATION)) Tf_Query
WHERE
Tf_Query.LOCATION=mi.LOCATION
于 2014-05-27T08:06:20.490 回答
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 item, MI.location, MI.card FROM mst_item MI INNER JOIN time_factor TF ON MI.location = TF.location AND MI.card = TF.card

于 2015-10-28T07:45:55.410 回答
-1

这可以通过使用左外连接来解决

于 2015-12-25T08:44:52.790 回答