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 |
|
sqldev6363
Yak Posting Veteran
54 Posts |
Posted - 2009-01-03 : 23:37:05
|
| Hi, I have a table tbl1,structure like this with some values. I have one more table tbl2. i want to update the values into this table from tbl1.i need the data result like this. table tbl1 containing data ID CT type G H 1 AB son 10 202 AB son 10 303 AB son 30 304 AB son 40 105 BC kod 60 606 BC kod 60 607 BC kod 100 308 CD can 50 80 9 CD can 20 9010 DE pan 70 60 I have 2 differenet percentages in G and H, i need to update into tbl2 based on the G and H combinations grouping by CT and Type. In the above table for AB or BC or any other CT the combination of the type is not only son or kod, they can have different types. for example for AB it can have kod or can also with different percentages.The result set should be like this.So i Need a query to update the tbl2, the below one is the tbl2 dataIn the columns G0, G10, G20,G30... count should be updated, if the combination is not there then it has to update 0For Example, in tbl1 we have a comnbination of AT and type is son and Percentage for col G is 10 and col H is 20.In the below table at the col g10 and in the column H row contains value 10 for the combination of AB and son,'1' shoulbe updated, because with that combination only 1 value is there in the tbl1. see in tbl1 id 6 and 7 contains same percentage for both G and H with the same combination. So in tbl2 count '2' shoulb be updated at the col g60 and at the row 60 with the cmobination of BC and Kod.Can any one help me in this, its very urgent CT type H G0 G10 G20 G30 G40 G50 G60 G70 G80 G90 G100 AB son 10 AB son 20 AB son 30 AB son 40 AB son 50 AB son 60AB son 70AB son 80AB son 90AB son 100 BC kod 10BC kod 20BC kod 30 BC kod 40BC kod 50BC kod 60 BC kod 70BC kod 80BC kod 90BC kod 100CD can 10CD can 20CD can 30CD can 40CD can 50CD can 60CD can 70CD can 80 CD can 90 CD can 100 . . .dev |
|
|
chrianth
Yak Posting Veteran
50 Posts |
Posted - 2009-01-04 : 23:01:26
|
| your sample data and scenario is quite unclear, although I came up with the update query below based on what I think what you want to put on your table2(the G'zzz thing based on CT,Type and H of Table1).HTH :)select count(ID) ID_count,CT,Type,H,Ginto #temp_table1_summaryfrom Table1group by CT,Type,H,Gupdate Table2set G0 = (case table1.G0 = 0 then #temp_table1_summ.ID_count else '0'),G10 = (case table1.G10 = 10 then #temp_table1_summ.ID_count else '0'),G20 = (case table1.G20 = 20 then #temp_table1_summ.ID_count else '0'),G30 = (case table1.G30 = 30 then #temp_table1_summ.ID_count else '0'),G40 = (case table1.G40 = 40 then #temp_table1_summ.ID_count else '0'),G50 = (case table1.G50 = 50 then #temp_table1_summ.ID_count else '0'),G60 = (case table1.G60 = 60 then #temp_table1_summ.ID_count else '0'),G70 = (case table1.G70 = 70 then #temp_table1_summ.ID_count else '0'),G80 = (case table1.G80 = 80 then #temp_table1_summ.ID_count else '0'),G90 = (case table1.G90 = 90 then #temp_table1_summ.ID_count else '0'),G100 = (case table1.G100 = 100 then #temp_table1_summ.ID_count else '0')from #temp_table1_summwhere #temp_table1_summ.CT = Table2.CTand #temp_table1_summ.Type = Table2.Typeand #temp_table1_summ.H = Table2.H |
 |
|
|
chrianth
Yak Posting Veteran
50 Posts |
Posted - 2009-01-04 : 23:06:42
|
| sorry...#temp_table1_summaryis same as #temp_table1_summi forgot to edit it before posting....also I haven't tested the query yet because im using a pc that doesnt have sql installed on it...sorry. |
 |
|
|
|
|
|
|
|