Join gets wrong information on query

0

Please login or register to vote for this query.

(click on this box to dismiss)

Anime and Manga

Q&A for anime and manga fans

;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

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results