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 |
|
KingCarlos
Yak Posting Veteran
74 Posts |
Posted - 2011-02-16 : 00:51:19
|
| Hi Can anyone help with this issue, my data is as follows Table = 1Field1 Field2a 2a 3b 7If I use the followingselect field1, (SUM(field2)) as total from support group by field1I get a = 5b = 7I now want to take these totals and populate them into a different tableTable 2 - Field 3How is that possible? |
|
|
KingCarlos
Yak Posting Veteran
74 Posts |
Posted - 2011-02-16 : 01:03:46
|
| Hi there, sorry I should be updating table 2 field 3so in the exampleTable 2 Field1 Field3a 5b 7 |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-16 : 01:06:58
|
| do you mean you want to insert the specified Same result set in two different tables, table1 and table2? Insert into table1 (ColName,ColName)select field1, (SUM(field2)) as total from support group by field1Insert into table2 (ColName,ColName)select field1, (SUM(field2)) as total from support group by field1 |
 |
|
|
KingCarlos
Yak Posting Veteran
74 Posts |
Posted - 2011-02-16 : 04:21:11
|
| Hi there, Thanks for the tips, so what I have done is the followingCreate tableCreate Table TableB (itemid varchar(55) null, F1 int null)Now as this data is continuely updating I now have 3 scripts to get the tables to update, all I need to figure out now is to create either a trigger or stored procedure to update every 4 hoursstep 1 - clean the tables out so I do not have any duplicatesdelete from TableB where f1 like '%%'step 2 - repopulate the holding tableInsert into tableb (itemid,f1)select itemid, (SUM(number1)) as total from support group by itemidstep 3 - update the table to contain the info.update itemssoldset user1 = (select f1 from TableB where tableb.itemid = itemssold.itemid) |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-16 : 05:26:12
|
| if there is no updates in the old records of your original table then perhaps you should schedule the following Insert into table1 (Col1Name,Col2Name)select field1, (SUM(field2)) as total from support Left Join table1 on table1.col1Name=Support.Field1Where table1.Col1Name is nullgroup by field1Insert into table2 (Col1Name,Col2Name)select field1, (SUM(field2)) as total from supportLeft Join table1 on table2.col1Name=Support.Field1Where table2.Col1Name is nullgroup by field1This will insert only newly added records CheersMIK |
 |
|
|
|
|
|
|
|