两个SQL查询中的值之间的差异

时间:2015-12-22 22:12:57

标签: sql-server difference

我在从两个不同的sql查询中获取值之间的实际差异时遇到问题。例如:

  • 查询1:

    SELECT x from db1 where date = 'mm-dd-yy'
    
  • 查询2:

    SELECT x from db1 where date = 'mm1-dd1-yy'
    

每个查询返回10个不同的值。我想要一个查询,它返回两个查询中值之间的实际差异(Q1中的Value1 - 来自Q2的Value1,类似于value2,依此类推)尝试EXCEPT,但这不起作用。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

尝试类似:

Select  x-x2 from
(
(SELECT c1,(case when date = 'mm1-dd1-yy' then c11 end)as x2,x from kdue)
)as y
where date = 'mm1-dd1-yy'

答案 1 :(得分:0)

如果我理解你的正确,请看下面的方法,如果它适合你。

select value1_From_QRY1-value1_From_QRY2 from (
   select row_cnt,sum(val1) as value1_From_QRY1,sum(va2) as value1_From_QRY2 from 
    (SELECT ROWNUM as row_cnt,x as val1 ,0 as val2 from db1 where date = 'mm-dd-yy' 
     union all
    SELECT ,ROWNUM ,0,x from db1 where date = 'mm1-dd1-yy')
   group by row_cnt)

答案 2 :(得分:0)

SELECT A.x1- B.x2 AS Difference FROM

(

(SELECT c, x1 from db1 where date = 'mm-dd-yy') AS A

JOIN

(SELECT c, x2 from db1 where date = 'mm1-dd1-yy') AS B

ON B.c = A.c

ORDER BY A.c, B.c