MS-SQL: Combining two identical tables, eliminate duplicate rows, SUM one column???

Posted on 2004-09-08
Medium Priority
Last Modified: 2008-01-09
  I have two identically structured tables. The data is different however, for one is a 'temp' table that is continually feed new data and the other is a 'result' table which is connected to a C# ListView. As you might have guessed, I need the 'temp' table to periodically add its data to the 'result' table to have a current SUM of all my data. It was too taxing on the ListVeiw to read from the same table the live data was feeding to, so I have two tables.

  If I use a UNION like so, I end up with ALL the data from both tables displayed seperately row by row, no adding or SUM taking place.

Table Structure:  First column is PRIKEY (INT 4), next four data columns (VARCHAR 50), last data column named 'counted' (INT 4).

Q. How do I combine two identically structured tables, eliminate dulplicate rows and SUM the last column 'counted'???

--select * from temp
--select * from result
Question by:kvnsdr
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
  • 3

Expert Comment

ID: 12006913
without the SUM:

select distinct * from temp
select distinct * from result

What does the "counted" actually contain?  Is it a count of records or a sum of data columns?

Author Comment

ID: 12007866

 That gives me the same result I don't want. The 'counted' column is a count of distinct occurances per row. All rows are usually different with a 'counted' number anywhere from 1 to 800. This small project of mine counts hits on a web site and monitors the users (source) DNS. So, concentrating on SUMing the 'counted' column is focal. I've been working on this following code which SUMs the 'counted' column, however I'm not effectually combining the data together.......

SELECT DISTINCT B.col_1, B.col_2, (A.counted) + (B.counted)
AS field3, B.col_4, B.col_5
FROM temp A, result B

Author Comment

ID: 12009024
To address this question more graphically, I've attempted to draw my tables.

table1 'result' (currently)

      col_IP1                     col_Dns1                       col_IP2                        col_Dns2                counted        whoknows.com                 herethere.com             200        yehyeh.com                      blahblah.com               100        youknowit.com                  jjsplace.com                  50

table2 'temp' (currently)

col_IP1                     col_Dns1                       col_IP2                        col_Dns2                   counted        whoknows.com                 herethere.com          100        yehyeh.com                      blahblah.com              50        youknowit.com                  jjsplace.com               25        thisisnewip.com                 anothernewip             35          // notice additional row

table1 'result' (goal results, correctly combined tables)

col_IP1                     col_Dns1                       col_IP2                        col_Dns2                    counted            // notice SUMed 'counted' rows        whoknows.com                 herethere.com            300        yehyeh.com                      blahblah.com              150        youknowit.com                  jjsplace.com                75        thisisnewip.com                 anothernewip              35

table1 'result' (I do NOT want this, duplicate rows. I used two SELECT and one UNION statement)

col_IP1                     col_Dns1                       col_IP2                        col_Dns2                     counted        whoknows.com                 herethere.com             200        yehyeh.com                      blahblah.com               100        youknowit.com                  jjsplace.com                  50        whoknows.com                 herethere.com             100        yehyeh.com                      blahblah.com                 50        youknowit.com                  jjsplace.com                  25        thisisnewip.com                 anothernewip                35


Author Comment

ID: 12009044
I increased the points because it's a tough one............. (:>)
LVL 12

Accepted Solution

kselvia earned 1000 total points
ID: 12009975
What about

Select  col_IP1,
      Sum(counted) counted
Select  col_IP1,
FROM temp
Union ALL
Select  col_IP1,
FROM result
) x

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

764 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