2个表的3个值之间的差异

时间:2012-02-14 01:30:11

标签: sql-server comparison

我有两张表,比如

id  att1 att2
1   34    56
2   45    12
3   34    11


id  att1 att2
1   14    10
2   25    2
3   14    10

如何区分3个值,如

id      att1      att2
1-1=0  34-14=20  56-10=46
2-2=0  45-25=20  12-2 =10  
3-3=0  34-14=20  11-10=1 


SELECT A.id-B.id, A.att1-B.att1,A.att2-B.att2 from TABLEA as A join TABLEB as B...

2 个答案:

答案 0 :(得分:3)

你真的不想从另一个中减去一个id,是吗?

SELECT
  A.id,
  A.att1 - B.att1,
  A.att2 - B.att2
FROM
  A JOIN B ON A.id = B.id

答案 1 :(得分:1)

不确定您在寻找问题时尚未提供的内容。你的意思是这样吗?

SELECT
    CAST(A.id AS VARCHAR(200)) + '-' +  CAST(B.id AS VARCHAR(200)) + '=' + CAST(A.id - B.id AS VARCHAR(200)) id
    ,CAST(A.att1 AS VARCHAR(200)) + '-' +  CAST(B.att1 AS VARCHAR(200)) + '=' + CAST(A.att1 - B.att1 AS VARCHAR(200)) att1
    ,CAST(A.att2 AS VARCHAR(200)) + '-' +  CAST(B.att2 AS VARCHAR(200)) + '=' + CAST(A.att2 - B.att2 AS VARCHAR(200)) att2
FROM
    TABLEA A
    INNER JOIN TABLEB B ON A.id = B.id