如果您能帮助我创建正确的sql,以便只检索一个值来匹配第三个表“table c”中的记录,我将不胜感激,如下所示。表A列出了客户ID、名称和状态。表B列出了外国主要客户的身份证和会员卡号码。表C列出了会员卡号码的销售额。
select A.cifno, A.cfna1, A.cfstate, B.hbmast.cifno, B.hbmast.hbcid, C.cast(ewmast.ewcid as dec(12,0)) as "eWire ID"
from library.cfmast cfmast join library.hbmast hbmast
on cfmast.cfcif# = hbmast.cifno left join library.ewmast ewmast
on hbmast.hbcid = cast(ewmast.ewcid as dec (12,0))
样本数据:
table A
**A.cifno, A.cfna1, A.cfstate**
J00022, John, OH
B00019, Ben, TX
table B
**B.hbmast.cifno, B.hbmast.hbcid**
J00022, 555500001234
B00019, 555500007878
table C
**ewmast.ewcid**
555500001234
555500001234
555500001234
555500007878
期望输出为:
J00022, John, OH, J00022, 555500001234, 555500001234
B00019, Ben, TX, B00019, 555500007878, 555500007878
--不是--
J00022, John, OH, J00022, 555500001234, 555500001234
J00022, John, OH, J00022, 555500001234, 555500001234
J00022, John, OH, J00022, 555500001234, 555500001234
B00019, Ben, TX, B00019, 555500007878, 555500007878
最佳答案
您应该在选择中使用关键字distinct
。
例如:
select distinct ....