我有一个联合查询,它从两个不同的请求中检索信息,比较它们并根据第三行的比较显示 YES、NO、NA。
下面是我的 SQL 查询
SELECT CAST(RR.reqestno AS VARCHAR(MAX)) reqestno,
CAST(RR.receiveddate AS VARCHAR(MAX)) AS receiveddate,
CAST(RR.rptcomments AS VARCHAR(MAX)) AS rptcomments,
CAST(RR.reportfrequency AS VARCHAR(MAX)) AS reportfrequency,
CAST(RR.schedule AS VARCHAR(MAX)) AS schedule,
CAST(RR.schedulebasedon AS VARCHAR(MAX)) AS schedulebasedon,
CAST(RR.clmsrvdtfrom AS VARCHAR(MAX)) AS clmsrvdtfrom,
CAST(RR.clmsrvdtthru AS VARCHAR(MAX)) AS clmsrvdtthru,
CAST(RR.clmpddtfrom AS VARCHAR(MAX)) AS clmpddtfrom,
CAST(RR.clmpddtthru AS VARCHAR(MAX)) AS clmpddtthru,
CAST(RR.customer AS VARCHAR(MAX)) AS customer
from REPORT_REQUEST RR
where RR.reqestno = xxxxxx
UNION ALL
SELECT CAST(RR.reqestno AS VARCHAR(MAX)) reqestno,
CAST(RR.receiveddate AS VARCHAR(MAX)) AS receiveddate,
CAST(RR.rptcomments AS VARCHAR(MAX)) AS rptcomments,
CAST(RR.reportfrequency AS VARCHAR(MAX)) AS reportfrequency,
CAST(RR.schedule AS VARCHAR(MAX)) AS schedule,
CAST(RR.schedulebasedon AS VARCHAR(MAX)) AS schedulebasedon,
CAST(RR.clmsrvdtfrom AS VARCHAR(MAX)) AS clmsrvdtfrom,
CAST(RR.clmsrvdtthru AS VARCHAR(MAX)) AS clmsrvdtthru,
CAST(RR.clmpddtfrom AS VARCHAR(MAX)) AS clmpddtfrom,
CAST(RR.clmpddtthru AS VARCHAR(MAX)) AS clmpddtthru,
CAST(RR.customer AS VARCHAR(MAX)) AS customer
from REPORT_REQUEST RR
where RR.reqestno = yyyyyy
UNION ALL
SELECT
CASE WHEN A.reqestno = B.reqestno THEN 'Yes' ELSE 'No' END as reqestno,
CASE WHEN A.receiveddate = B.receiveddate THEN 'Yes' ELSE 'No' END as receiveddate,
CASE WHEN A.rptcomments is null and B.rptcomments is null then 'NA' WHEN A.rptcomments = B.rptcomments THEN 'Yes' ELSE 'No' END rptcomments,
CASE WHEN A.reportfrequency = B.reportfrequency THEN 'Yes' ELSE 'No' END as reportfrequency,
CASE WHEN A.schedule = 'NONE' and B.schedule = 'NONE' THEN 'NA'WHEN A.schedule = B.schedule THEN 'Yes' ELSE 'No' END as schedule,
CASE WHEN A.schedulebasedon = 'NONE' and B.schedulebasedon = 'NONE' THEN 'NA' WHEN A.schedulebasedon = B.schedulebasedon THEN 'Yes' ELSE 'No' END as schedulebasedon,
CASE WHEN A.clmsrvdtfrom is null and B.clmsrvdtfrom is Null Then 'NA' WHEN A.clmsrvdtfrom = B.clmsrvdtfrom THEN 'Yes' ELSE 'No' END as clmsrvdtfrom,
CASE WHEN A.clmsrvdtthru is null and B.clmsrvdtthru is null then 'NA' WHEN A.clmsrvdtthru = B.clmsrvdtthru THEN 'Yes' ELSE 'No' END as clmsrvdtthru,
CASE WHEN A.clmpddtfrom is Null and B.clmpddtfrom is null then 'NA' WHEN A.clmpddtfrom = B.clmpddtfrom THEN 'Yes' ELSE 'No' END as clmpddtfrom,
CASE WHEN A.clmpddtthru is null and B.clmpddtthru is null then 'NA' WHEN A.clmpddtthru = B.clmpddtthru THEN 'Yes' ELSE 'No' END as clmpddtthru,
CASE WHEN A.customer = B.customer THEN 'Yes' ELSE 'No' END as customer
FROM
(SELECT reqestno,receiveddate,rptcomments,reportfrequency,schedule,schedulebasedon,clmsrvdtfrom,
clmsrvdtthru,clmpddtfrom,clmpddtthru,customer FROM REPORT_REQUEST WHERE reqestno = xxxxxx) AS A
INNER JOIN
(SELECT reqestno,receiveddate,rptcomments,reportfrequency,schedule,schedulebasedon,clmsrvdtfrom,
clmsrvdtthru,clmpddtfrom,clmpddtthru,customer FROM REPORT_REQUEST WHERE reqestno = yyyyyy) AS B
ON 1 = 1
问题 我应该如何编辑上述查询,使其在比较前两行后仅显示第三行中只有值为“NO”的列,并以某种方式隐藏其他列,只要比较返回 YES 或 NA。我想要查看所有 3 行(2 行数据 + 1 行比较
需要帮助,请提前谢谢