I have two tables Sell and Purchase. My query is giving me the desired result, I am carefull about it performance so plz guide me if it can be better. My tables are:
Table Sell
UserId | ProductId | ProductName | ProductPrice
1 | p_101 | Cycle | 500
1 | p_121 | Car | 500000
2 | p_111 | Cycle | 5000
Table Purchase
UserId | ProductId | ProductName | ProductPrice
1 | p_109 | CellPhone | 150
2 | p_121 | Car | 500000
3 | p_111 | Book | 15
Desired OutPut Table
Type | ProductId | ProductName | ProductPrice
Sell | p_101 | Cycle | 500
Sell | p_121 | Car | 500000
Purchase| p_109 | CellPhone | 150
Working Query:
SELECT type, P1.ProductId, P1.ProductName, P1.ProductPrice
FROM
(
SELECT s.UserId, 'Sell' as type, s.ProductId, s.ProductName, s.ProductPrice FROM [Sell] s
UNION
SELECT p.userid, 'Purchase' as type, p.ProductId, p.ProductName, p.ProductPrice FROM [Purchase] p
) as P1
WHERE userid=1