Solved

MSSQL Insert into Table from another Table that some fields are SUM()

Posted on 2011-03-18
4
744 Views
Last Modified: 2012-05-11
Hello,

I have a table, OrigTable, with 8 fields in it Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8.  I want to Sum() Field7 and Filed8 using the Group By Field1, Field2, Field3, Field4. Then insert all 8 Fields into NewTable as Field1, Field2, Field3, Field4, Field5, Field6, Field7 (sum value), Field8 (sum value).  The Unique key in NewTable is Field1, Field2, Field3, Field4.  Field5 adn Field6 could have different values but it doesn't matter which value is used the field to insert the record in NewTable.  Is this possible?

Thanks.
0
Comment
Question by:JohnJMA
4 Comments
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
It should be something like this: (you also need to group by Field5 and Field6)

Select Field1, Field2, Field3, Field4, Field5, Field6, Sum(Field7) Field7Sum, Sum(Field8) Field8Sum
  Into NewTable
  From OrigTable
 Group By Field1, Field2, Field3, Field4, Field5, Field6

Open in new window

0
 
LVL 11

Expert Comment

by:JoeNuvo
Comment Utility
problem is, if you want Field1 - 4 as "unique key" in NewTable
then you must do something with Field5, Field6 as well.
for ex. pick one of the value

INSERT INTO NewTable
SELECT Field1, Field2, Field3, Field4, MIN(Field5) AS Field5, MIN(Field6) AS Field6, SUM(Field7) AS Field7, SUM(Field8) AS Field8
FROM OrigTable
GROUP BY Field1, Field2, Field3, Field4

Open in new window

0
 
LVL 4

Accepted Solution

by:
samijsr earned 500 total points
Comment Utility
As of your second Table Field5 and 6 are not Unique key, then in this case you have to manipulate the data of Field5 and 6 as you can not use simple Group by clause for all 6 fileds as It may casue duplicate the Field 1,2,3,4.
Now u did not mention data type of Field5 and 6 if it's numeric you can use sum() or any other function
other wise
Insert Into NewTable
Select distinct T1.Field1, T1.Field2, T1.Field3, T1.Field4,T2.Field5,T2.Field6,Field7sum,Field8Sum
(
Select Field1, Field2, Field3, Field4,  Sum(Field7) Field7Sum, Sum(Field8) Field8Sum
    From OrigTable  Group By Field1, Field2, Field3, Field4)T1
left inner Join
(Select Field1, Field2, Field3, Field4, Field5, Field6, Sum(Field7) , Sum(Field8)  
  From OrigTable  Group By Field1, Field2, Field3, Field4, Field5, Field6 )T2
on T1.Field1=T2.Field1 and T1.Field2=T2.Field2 and T1.Field3=T2.Field3 and T1.Field4=T2.Field4
0
 

Author Closing Comment

by:JohnJMA
Comment Utility
I did get it to work wiht your example samijsr.  And Filed5 and 6 are not numeric.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now