繁体   English   中英

SQL查询以棘手的逻辑联接两个表

[英]Sql Query to join two table with tricky logic

我有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表的位置和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

请帮助我查询逻辑。

我认为这应该可以解决问题...

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);

要在仅位置匹配时返回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;

OUTPUT

在此处输入图片说明

更好的查询可以是

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;

OUTPUT

在此处输入图片说明

另一种变化可以是

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
;

OUTPUT

在此处输入图片说明

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

SQL小提琴演示位于http://sqlfiddle.com/#!2/53e53/21

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

这是您要找的东西吗?

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

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

从mst_item mst,time_factor tym中选择项,mst.location,decode(mst.card,tym.card,tym.card,'ALL'),其中mst.location = tym.location;

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

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

选择项,MI.location,MI.card FROM mst_item MI INNER JOIN time_factor TF ON MI.location = TF.location AND MI.card = TF.card

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM