Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rkalyani
Starting Member
30 Posts |
Posted - 2009-03-05 : 13:17:19
|
| I need to write a sql query to do this but i am not sure how to do it.I have a table like thisprd Smkr Gender Rate Age11 N F 100 2911 N F 150 30 11 N F 150 3111 N F 150 3211 N 50 011 N 75 1Now I need to update rows for product 12. I need it as an update because I already have rows in it. THe update should just caluculate different rate for a different product number but the rest of the information will be same. So the result will be like thisprd Smkr Gender Rate Age12 N F 120 2912 N F 160 30 12 N F 160 3112 N F 160 3212 N 60 012 N 80 1Is there a SQL way to do this. I wrote code in .net to loop through and do it but I was wondering if I could write a sql query to do this.Thank you |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-05 : 13:20:18
|
| you can use UPDATE table SET...WHERE prd=12 to update all the records with product 12 in a batch. |
 |
|
|
rkalyani
Starting Member
30 Posts |
Posted - 2009-03-05 : 13:25:28
|
| Sorry, I should have been clear. I already have the rows in the first table - prd_id =11. Now I need to go through every row in prd_id 11 and update the values in prd_id 12 with the new rate. I will get the new rate as a multiple of the rate in prd_id 11. So the columns and most of the values are the same except the rate.Thank youThank you,kal30 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-05 : 13:30:32
|
then just doUPDATE t1SET t1.Rate=t2.Rate* your multiplying factorFROM Table t1JOIN table t2ON t1.Age=t2.AgeWHERE t1.prd=12AND t2.prd=11 |
 |
|
|
rkalyani
Starting Member
30 Posts |
Posted - 2009-03-05 : 13:44:14
|
| One more question. Both the fields are in the same table. I mean prd_id 11 and prd_id 12 are in the same table. Can I join values in same table.Thank youThank you,kal30 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-07 : 13:48:00
|
| yup you can . as you see in my query. i'm using same table twice. just use different aliases (t1 & t2 in my example) |
 |
|
|
|
|
|