选择查询,其中列不等于SQL Server中的不同表中的列
问题描述:
我有两个包含订单/定价数据的不同表。第一个表Orders
的列有order
,item
和price
。第二个表Pricing
有列item
,price
和effective 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不具备最先进的最新价格。
请指教。
答
它应该是这样的: -
SELECT * FROM Orders O
INNER JOIN Pricing P ON O.Item = P.Item
Where O.Price != P.Price AND [Effective Date] ='07/27/2017'
答
你在找这样的吗?
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
输出:
| Order | |-------| | 2 | | 3 | | 4 |
答
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)
答
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
首先,你需要从第二个表中写出一个查询,显示最新的价格。什么版本的SQL Server?你到目前为止尝试过什么 –
谢谢你,这是我需要让我走向正确的方向!明白了。 – jmarusiak