0

我有一个联合查询,它从两个不同的请求中检索信息,比较它们并根据第三行的比较显示 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 行比较

需要帮助,请提前谢谢

4

2 回答 2

0

您可能希望将您的ON表达式更改INNER JOIN为由 连接的“否”条件and

于 2013-03-06T01:13:10.673 回答
0

基本上,您将 UNION 查询放入子查询并对其进行限制。我不会复制你的怪物查询——请阅读 SSCCE(简短、独立、正确的示例)——但是:

 SELECT r.*
   FROM (SELECT Value1, Value2
           FROM SomeWhere
          WHERE SomeColumn = 234
         UNION
         SELECT Value1, Value2
           FROM ElseWhere
          WHERE ElseColumn = 432
        ) AS r
  WHERE Value1 > 37 OR Value2 < 91

您可能还需要对 OLAP 代码做一些事情;您提到有问题的“第三行”-SQL 是一种基于集合的语言,并且排序不自然(但可以做到)。

于 2013-03-06T01:16:42.467 回答