1

我有大量数据需要根据价格进行更新。很简单,Code/Size 1 中的商品的价格必须等于或小于 Code/Size 2。Code/Size 2 必须等于或小于 Code/Size 3,依此类推:

Item       Code   Size  Price   
-------------------------------
66227019    OTH     1   $466
66227019    OTH     2   $486
66227019    OTH     3   $476
66227019    OTH     4   $537

66227019    IVC     1   $458
66227019    IVC     2   $428
66227019    IVC     3   $458
66227019    IVC     4   $458

必须看起来像:

Item       Code   Size  Price   
-------------------------------
66227019    OTH     1   $466
66227019    OTH     2   $486
66227019    OTH     3   $486
66227019    OTH     4   $537

66227019    IVC     1   $458
66227019    IVC     2   $458
66227019    IVC     3   $458
66227019    IVC     4   $458

我已经尝试了一些自我加入,但我基本上失去了理智。真诚感谢任何帮助。

谢谢!

4

4 回答 4

3

对于 SQL-Server 2012,您可以使用:

; WITH cte AS
( SELECT *,
         MaxPrice = MAX(Price) 
                    OVER (PARTITION BY Item, Code
                          ORDER BY Size
                          ROWS BETWEEN UNBOUNDED PRECEDING
                                   AND 1 PRECEDING
                         ) 
  FROM Products
) 
UPDATE cte
SET Price = MaxPrice
WHERE Price < MaxPrice ;

SQL-Fiddle测试

于 2013-10-14T22:03:06.353 回答
1

这能满足您的需要吗?

UPDATE LaterRec
SET Price = PrevRec.Price
FROM Data AS LaterRec
    INNER JOIN Data AS PrevRec ON LaterRec.Item = PrevRec.Item AND LaterRec.Code = PrevRec.Code AND PrevRec.Size < LaterRec.Size
WHERE
        PrevRec.Price > LaterRec.Price
    -- Disregard this entry if there's a record *between* PrevRec and LaterRec
    AND NOT EXISTS (SELECT * FROM Data AS Intrmdt WHERE LaterRec.Item = Intrmdt.Item AND LaterRec.Code = Intrmdt.Code AND Intrmdt.Size > PrevRec.Size AND Intrmdt.Size < LaterRec.Size)
于 2013-10-14T22:00:51.450 回答
1

答案是递归 CTE。不取决于 SQL SERVER 版本。

    DECLARE @t TABLE
    (
        item        INT
        ,code       sysname
        ,SIZE       INT
        ,price      INT
    )
    INSERT INTO @t( item, code, SIZE, price )
                SELECT 1,'oth',1,466
    UNION ALL   SELECT 1,'oth',2,486
    UNION ALL   SELECT 1,'oth',3,476
    UNION ALL   SELECT 1,'oth',4,537

    UNION ALL   SELECT 1,'ivc',1,458
    UNION ALL   SELECT 1,'ivc',2,428
    UNION ALL   SELECT 1,'ivc',3,458
    UNION ALL   SELECT 1,'ivc',4,458

    SELECT * FROM @t

    ;WITH cte1 (item,code,size,price,rownum1,rownum2)
    AS
    (
        SELECT *, ROW_NUMBER() OVER (PARTITION BY item,code ORDER BY item,code,SIZE) AS rownum1, ROW_NUMBER() OVER (ORDER BY item,code,SIZE) AS rownum2
        FROM @t
    ),  
    cte2 (item,code,size,price,rownum1,rownum2)
    AS
    (
        SELECT *
        FROM cte1 t1
        WHERE rownum2=1

        UNION ALL

        SELECT t1.item,t1.code,t1.size
                ,CASE   WHEN t1.price>=t2.price THEN t1.price 
                        WHEN t1.price<t2.price THEN t2.price
                END AS proce
                ,t1.rownum1,t1.rownum2
        FROM cte2 t2
        JOIN cte1 t1
            ON t2.rownum2+1=t1.rownum2
    )
    SELECT item,code,size,price FROM cte2

在此处输入图像描述

于 2013-10-14T22:11:13.410 回答
0

假设t是您的表的名称:

update t set price=p
from (
  select nxt.item i,nxt.code c,nxt.size s,max(cur.price) p
  from t cur join t nxt 
  on (cur.item=nxt.item and cur.code=nxt.code 
   and cur.size<nxt.size and cur.price>nxt.price)
  group by nxt.item,nxt.code,nxt.size      
  ) a
where item=i and code=c and size=s
于 2013-10-14T22:28:05.193 回答