2017-08-01 72 views
0

我有兩個包含訂單/定價數據的不同表。第一個表Orders的列有order,itemprice。第二個表Pricing有列itempriceeffective date。我想查找所有訂單,其中orders.price <>pricing.price其中生效日期= 2017年7月27日。選擇查詢,其中列不等於SQL Server中的不同表中的列

訂單

Order Item Price 
--------------------- 
1  ABC  12.50 
2  ABC  12.00 
3  ABC  11.50 
4  XYZ  20.00 

定價

Item Price Effective Date 
------------------------------- 
ABC  12.50 7/27/2017 
ABC  12.00 12/1/2016 
ABC  11.50 12/1/2015 
XYZ  25.00 7/27/2017 
XYZ  20.00 12/1/2016 

我希望查詢告訴我,爲了2,3,4不具備最先進的最新價格。

請指教。

+0

首先,你需要從第二個表中寫出一個查詢,顯示最新的價格。什麼版本的SQL Server?你到目前爲止嘗試過什麼 –

+0

謝謝你,這是我需要讓我走向正確的方向!明白了。 – jmarusiak

回答

0

它應該是這樣的: -

SELECT * FROM Orders O 
INNER JOIN Pricing P ON O.Item = P.Item 
Where O.Price != P.Price AND [Effective Date] ='07/27/2017' 
0

你在找這樣的嗎?

SELECT o.[Order] 
    FROM Orders o JOIN Pricing p 
    ON o.Item = p.Item 
    AND p.Effective_Date = '2017-07-27' 
    AND o.price <> p.price 

SQLFiddle演示

輸出:

 
| Order | 
|-------| 
|  2 | 
|  3 | 
|  4 | 
0
select o.order,o.Item,o.price from orders o inner join 
pricing on o.item=pricing.item 
where o.price <>(select p.price from pricing p where p.effectivedate = '2017-07-27' and p.item=o.item) 
1
SELECT O.ORDER, (P.PRICE - O.PRICE) AS 'Variance' 
FROM ORDERS O, PRICING P 
WHERE O.ITEM = P.ITEM 
AND P.[EFFECTIVE DATE] = '7/27/2017' 
AND P.PRICE <> O.PRICE 
ORDER BY O.ORDER ASC 
相關問題