を使用した方がよいという@JNKのコメントに同意しますVIEW
。SQL Server 2005+ の場合、次を試すことができます。
CREATE VIEW LowPrices
AS
SELECT A.store, A.item, A.price, A.Low_Price, MIN(B.store) Low_Price_Store
FROM ( SELECT *, MIN(price) OVER(PARTITION BY item) Low_Price
FROM YourTable) A
JOIN YourTable B
ON A.Low_Price = B.price
GROUP BY A.store, A.item, A.price, A.Low_Price
それでもやりたい場合はUPDATE
、次を試してください。
WITH CTE AS
(
SELECT A.store, A.item, A.price, A.Low_Price, MIN(B.store) Low_Price_Store
FROM ( SELECT *, MIN(price) OVER(PARTITION BY item) Low_Price
FROM YourTable) A
JOIN YourTable B
ON A.Low_Price = B.price
GROUP BY A.store, A.item, A.price, A.Low_Price
)
UPDATE A
SET A.LowPrice = B.Low_Price,
A.LowPriceStore = B.Low_Price_Store
FROM YourTable A
JOIN CTE B
ON A.store = B.Store AND A.item = B.item