加入获取有关查询的错误信息

时间:2012-01-26 15:28:57

标签: sql sql-server-2008

我在加入查询方面遇到了一些问题。我想要的是选择具有一些相似状态的两个表的行,其他没有。我有一个状态等值视图

Table Sales:
ID  .....   StateID
1           1
2           1
3           6

Table Orders
ID  .....   StateID
11          2
12          2
15          3

Table StatesEquivalence
ID  SalesState   OrdersState    StateName
1   1            2              Attended
2   2            3              Declined

我已经建立了两个表(Sales和Orders)的联合,我想要一个具有EquivalentStateID和StateName的列,通过这样做:

SELECT sales.ID as ID,equivalence.ID as State,equivalence.StateName
FROM Sales
INNER JOIN StatesEquivalence as equivalence
ON sales.StateID = equivalence.SalesState
WHERE sales.ID = 1
UNION
SELECT orders.ID as ID,equivalence.ID as State,equivalence.StateName
FROM Orders as orders
INNER JOIN StatesEquivalence as equivalence
ON orders.StateID = equivalence.OrdersState

不知何故,我在equivID

上获得了错误的信息
ID      State        StateName
1       2            Attended
2       2            Attended
11      2            Attended
...

我不知道发生了什么..因为statename是正确的,但StateID显示错误的信息

表格可能会显示:

ID      State        StateName
1       1            Attended
2       1            Attended
11      1            Attended
...

3 个答案:

答案 0 :(得分:1)

我认为您没有向我们展示真正的问题代码。我做了以下事情:

begin transaction

create table sales (id int, stateid int)
create table orders (id int, stateid int)
create table statesequivalence (id int, salesstate int, ordersstate int, statename varchar(20))

insert into sales values (1, 1)
insert into sales values (2, 1)
insert into sales values (3, 6)

insert into orders values (11, 2)
insert into orders values (12, 2)
insert into orders values (15, 3)

insert into statesequivalence values (1, 1, 2, 'Attended')
insert into statesequivalence values (2, 2, 3, 'Declined')

SELECT sales.ID as ID,equivalence.ID as State,equivalence.statename
FROM Sales
INNER JOIN statesequivalence as equivalence
ON sales.StateID = equivalence.SalesState
WHERE sales.ID = 1
UNION
SELECT orders.ID as ID,equivalence.ID as State,equivalence.StateName
FROM Orders as orders
INNER JOIN statesequivalence as equivalence
ON orders.StateID = equivalence.OrdersState

rollback

结果:

ID          State       statename
----------- ----------- --------------------
1           1           Attended
11          1           Attended
12          1           Attended
15          2           Declined

所以,它有效。你能试试上面的代码吗?

答案 1 :(得分:1)

以下是一份测试文件和你的陈述的输出 也许从这里工作更容易,你告诉我们这个输出有什么问题。

可以调整并执行测试文件here

<强>输出

ID          State       StateName
----------- ----------- ---------
1           1           Attended
11          1           Attended
12          1           Attended
15          2           Declined

测试脚本

;WITH Sales AS (
  SELECT * FROM (VALUES
    (1, 1)
    , (2, 1)
    , (3, 6)
  ) AS Sales (ID, StateID)
)
, Orders AS (
  SELECT * FROM (VALUES
    (11, 2)
    , (12, 2)
    , (15, 3)
  ) AS Orders (ID, StateID)
)
, StatesEquivalence AS (
  SELECT * FROM (VALUES
    (1, 1, 2, 'Attended')
    , (2, 2, 3, 'Declined')
  ) AS StatesEquivalence (ID, SalesState, OrdersState, StateName)
)
SELECT  sales.ID as ID
        , equivalence.ID as State
        , equivalence.StateName
FROM    Sales
        INNER JOIN StatesEquivalence as equivalence ON sales.StateID = equivalence.SalesState
WHERE   sales.ID = 1
UNION
SELECT  orders.ID as ID
        , equivalence.ID as State
        , equivalence.StateName
FROM    Orders as orders
        INNER JOIN StatesEquivalence as equivalence ON orders.StateID = equivalence.OrdersState      

答案 2 :(得分:0)

我完全不明白你需要什么,但也许是这个?

select *
from StatesEquivalence EQ
full join Sales S on EQ.SalesState = S.StateId
full join Orders O on EQ.OrdersState = O.StateId

这可能是一个起点...