从联合查询中限制列

时间:2013-03-06 00:24:15

标签: sql

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

请帮助 提前致谢

2 个答案:

答案 0 :(得分:0)

您可能希望将ON的{​​{1}}表达式更改为INNER JOIN关联的“否”条件。

答案 1 :(得分:0)

基本上,您将UNION查询放入子查询并限制它。我不会复制你的怪物查询 - 请阅读SSCCE(Short, Self-Contained, Correct Example) - 但是:

 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是一种基于集合的语言,排序不自然(但可以做到)。