Solved

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

Posted on 2011-03-18
4
785 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35168707
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
ID: 35169700
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
ID: 35170498
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
ID: 35290347
I did get it to work wiht your example samijsr.  And Filed5 and 6 are not numeric.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

695 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