SQL standard and “dependent” conditionals in UPDATE

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

CREATE TABLE #t1
( col1 INT NOT NULL
, col2 INT NOT NULL
) ;

INSERT INTO #t1
VALUES
( 9, 1) ,
( 8, 2) ,
(10, 2) ;


UPDATE p1
    SET col1 = col1 * 2
    FROM  #t1 AS p1
    WHERE col1 <= (SELECT AVG(col1) FROM #t1 WHERE col2=p1.col2);

SELECT *
FROM #t1 ;

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results