我有两个表,其字段分别为email,pan,mobile no,folio_NO。两个表都具有相同的字段。表名之一是直接的,另一个是负的。我必须按字段明智地比较两个表,例如电子邮件和电子邮件,必须平移...必须进行比较,如果电子邮件匹配,则不应在输出中显示该电子邮件。输出应类似于电子邮件列表,而不是我在下面的查询中尝试过但仍匹配的emailid和Pan号被检索到的情况中,在Negative表中提到的Negative表以及不在Native中的电话号码列表,不在Negative中的泛号列表。有人可以帮忙吗
Select
(Case when Direct.Mobile_D=NL_Mobile_N THEN NULL ELSE MOBILE_D END)
(Case when Direct.PAN_D=NL_PAN_N THEN NULL ELSE MOBILE_D END)
(Case when Direct.FOLIO_D=NL_FOLIO_N THEN NULL ELSE MOBILE_D END)
(Case when Direct.EMAIL_D=NL_EMAIL_N THEN NULL ELSE MOBILE_D END)
From
(
(
SELECT ltrim(rtrim(upper(Mobile))) AS
Mobile_D,ltrim(rtrim(upper(PAN))) AS PAN_D,ltrim(rtrim(upper(FOLIO))) AS
FOLIO_D,ltrim(rtrim(upper(EMAIL_ID))) AS EMAIL_D
FROM DIRECT_Table
)DIRECT---table 1
LEFT OUTER JOIN
(
SELECT ltrim(rtrim(upper(Mobile))) AS Mobile_D,ltrim(rtrim(upper(PAN)))
AS PAN_D,ltrim(rtrim(upper(FOLIO))) AS
FOLIO_D,ltrim(rtrim(upper(EMAIL_ID))) AS EMAIL_D
FROM NEGATIVE_Table---table2
)NL
ON Direct.Mobile_D=NL_Mobile_N
AND Direct.PAN_D=NL_PAN_N
AND Direct.FOLIO_D=NL_FOLIO_N
AND Direct.EMAIL_D=NL_EMAIL_N
)
GROUP BY
(Case when Direct.Mobile_D=NL_Mobile_N THEN NULL ELSE MOBILE_D END)
(Case when Direct.PAN_D=NL_PAN_N THEN NULL ELSE MOBILE_D END)
(Case when Direct.FOLIO_D=NL_FOLIO_N THEN NULL ELSE MOBILE_D END)
(Case when Direct.EMAIL_D=NL_EMAIL_N THEN NULL ELSE MOBILE_D END)
答案 0 :(得分:0)
您的问题措辞不好,对不起。您是否必须每列输出任何不匹配的行或不匹配的值? (例如,如果电子邮件匹配,但电话号码不匹配)。我假设下面是基于行的比较。
根据所使用的数据库,您可能有权访问EXCEPT子句,例如: https://www.postgresql.org/docs/current/queries-union.html
示例查询:
SELECT phone, email FROM D_tab
EXCEPT
SELECT phone, email FROM N_tab
这将为您提供D_tab
中未出现的来自N_tab
的行