Sql Query使用棘手的逻辑加入两个表

时间:2013-11-07 09:59:07

标签: sql oracle

我有2个表mst_item和time_factor,mst_item有3列项目,location,card和time_factor有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表位置和卡都匹配,则输出应为 在time_factor表中的位置和卡然后它应该返回匹配的记录,

ex-for R10& CRU

item     location    card
xyz       R10        CRU

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

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

前R14和NAC

item     location    card
CCC       R14        ALL

请帮我查询逻辑。

7 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

SQL QUERY

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

ORACLE QUERY

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;

<强>输出

enter image description here

更好的查询可以是

SQL QUERY

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

ORACLE QUERY

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

<强>输出

enter image description here

另一种变体可以是

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 QUERY

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) != '';

ORACLE QUERY

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
;

<强>输出

enter image description here

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

http://sqlfiddle.com/#!2/53e53/21

上的SQL小提琴演示

Oracle http://sqlfiddle.com/#!4/d28b1/26

上的小提琴演示

答案 2 :(得分: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

答案 3 :(得分:0)

根据我对要求的理解,以下查询将达到目的:

选择项目,mst.location,decode(mst.card,tym.card,tym.card,'ALL') 从     mst_item mst,     time_factor tym 哪里     mst.location = tym.location;

答案 4 :(得分: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

答案 5 :(得分:0)

SELECT item,MI.location,MI.card 来自mst_item MI INNER JOIN time_factor TF ON MI.location = TF.location AND MI.card = TF.card

答案 6 :(得分:-1)

这可以通过使用左外连接

来解决