Author |
Topic |
daidaluus
Yak Posting Veteran
73 Posts |
Posted - 2008-11-24 : 05:15:59
|
I want to update a table based on a sort on another table.CREATE TABLE t1 (c1 int, c2 int)CREATE UNIQUE INDEX [t1_Idx] ON t1 (c2)CREATE TABLE t2 (col1 int, col2 int)CREATE UNIQUE INDEX [t2_Idx] ON t2 (col1)GOINSERT INTO t1SELECT 1, 5 UNION ALLSELECT 2, 4 UNION ALLSELECT 3, 7 UNION ALLSELECT 4, 1 UNION ALLSELECT 5, 9 UNION ALLSELECT 6, 6 UNION ALLSELECT 7, 8INSERT INTO t2SELECT 1, 0 UNION ALLSELECT 2, 0 UNION ALLSELECT 3, 0 UNION ALLSELECT 4, 0 UNION ALLSELECT 5, 0 UNION ALLSELECT 6, 0 UNION ALLSELECT 7, 0DECLARE @val intSET @val = 0now i want to update col2 of t2 based on the sort of c1 of t1 to get the followning result:col1 col2 ----------- ----------- 1 52 43 74 15 96 67 8i tried this:SET @val = 0UPDATE aSET col2 = @val, @val = @val + 1FROM t2 aINNER JOIN t1 b WITH (INDEX (t1_Idx)) ON a.col1 = b.c1but it doesnt work. can anybody help me? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-24 : 05:26:55
|
dont you need only this?UPDATE t2SET t2.col2=t1.c2FROM t2JOIN t1ON t1.c1=t2.col1 |
 |
|
daidaluus
Yak Posting Veteran
73 Posts |
Posted - 2008-11-24 : 05:45:04
|
no it is not what i want. lets change the content of t1 as followINSERT INTO t1SELECT 1, 8 UNION ALLSELECT 2, 12 UNION ALLSELECT 3, 20 UNION ALLSELECT 4, 3 UNION ALLSELECT 5, 7 UNION ALLSELECT 6, 17 UNION ALLSELECT 7, 9now i want to update col2 in t2 with a serial number starting with 1. as you can see i used a variable to update this column. i want to increment this variable and assign it to col2 according the sort of c2 in t1. that is, when i to have col2 = 1 for col1 = 4 because the c1 = 4 has the least value for c2 in t1. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-24 : 05:56:57
|
quote: Originally posted by daidaluus no it is not what i want. lets change the content of t1 as followINSERT INTO t1SELECT 1, 8 UNION ALLSELECT 2, 12 UNION ALLSELECT 3, 20 UNION ALLSELECT 4, 3 UNION ALLSELECT 5, 7 UNION ALLSELECT 6, 17 UNION ALLSELECT 7, 9now i want to update col2 in t2 with a serial number starting with 1. as you can see i used a variable to update this column. i want to increment this variable and assign it to col2 according the sort of c2 in t1. that is, when i to have col2 = 1 for col1 = 4 because the c1 = 4 has the least value for c2 in t1.
so what will be your t2 values for this t1 data? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-24 : 06:04:54
|
is it this what you're looking at?UPDATE t2SET t2.col2=t1.seqFROM t2JOIN (SELECT (SELECT COUNT(*) FROM t1 WHERE c2 <=t.c2) AS Seq,*FROM t1 t)t1ON t1.c1=t2.col1output-------------------------------col1 col21 32 23 54 15 76 47 6 |
 |
|
daidaluus
Yak Posting Veteran
73 Posts |
Posted - 2008-11-24 : 06:27:36
|
yes. that's exactly what i need. thanks. can you tell me about the performance of this query when i use it for a large amount of data? is there any way to improve it? |
 |
|
daidaluus
Yak Posting Veteran
73 Posts |
Posted - 2008-11-24 : 06:45:20
|
when i have duplicate values for for c2 (no unique constraint) i get duplicate values for seq. is there any workaround? i need a unique sequence of numbers. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-24 : 06:47:31
|
quote: Originally posted by daidaluus yes. that's exactly what i need. thanks. can you tell me about the performance of this query when i use it for a large amount of data? is there any way to improve it?
yup. the subquery will perform badly in case of large data. b/w are you using sql 2005? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-24 : 06:48:40
|
quote: Originally posted by daidaluus when i have duplicate values for for c2 (no unique constraint) i get duplicate values for seq. is there any workaround? i need a unique sequence of numbers.
If you were using sql 2005 this would have been much easire using window functions. in case of duplicate values you might need a temporary table. |
 |
|
|