Solved

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

Posted on 2004-09-08
5
1,017 Views
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
--union
--select * from result
0
Comment
Question by:kvnsdr
  • 3
5 Comments
 
LVL 7

Expert Comment

by:SQL_Stu
ID: 12006913
without the SUM:

select distinct * from temp
union
select distinct * from result

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

Author Comment

by:kvnsdr
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
0
 
LVL 1

Author Comment

by:kvnsdr
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

123.123.123.123        whoknows.com              212.213.214.215             herethere.com             200
111.222.111.222        yehyeh.com                   222.236.225.253             blahblah.com               100
241.126.142.212        youknowit.com               121.245.241.121             jjsplace.com                  50


table2 'temp' (currently)

col_IP1                     col_Dns1                       col_IP2                        col_Dns2                   counted

123.123.123.123        whoknows.com              212.213.214.215             herethere.com          100
111.222.111.222        yehyeh.com                   222.236.225.253             blahblah.com              50
241.126.142.212        youknowit.com               121.245.241.121             jjsplace.com               25
222.222.222.222        thisisnewip.com              212.214.218.145             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

123.123.123.123        whoknows.com              212.213.214.215             herethere.com            300
111.222.111.222        yehyeh.com                   222.236.225.253             blahblah.com              150
241.126.142.212        youknowit.com               121.245.241.121             jjsplace.com                75
222.222.222.222        thisisnewip.com              212.214.218.145             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

123.123.123.123        whoknows.com              212.213.214.215             herethere.com             200
111.222.111.222        yehyeh.com                   222.236.225.253             blahblah.com               100
241.126.142.212        youknowit.com               121.245.241.121             jjsplace.com                  50
123.123.123.123        whoknows.com              212.213.214.215             herethere.com             100
111.222.111.222        yehyeh.com                   222.236.225.253             blahblah.com                 50
241.126.142.212        youknowit.com               121.245.241.121             jjsplace.com                  25
222.222.222.222        thisisnewip.com              212.214.218.145             anothernewip                35


0
 
LVL 1

Author Comment

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

Accepted Solution

by:
kselvia earned 250 total points
ID: 12009975
What about

Select  col_IP1,
      col_Dns1,
      col_IP2,
      col_Dns2,
      Sum(counted) counted
From
(
Select  col_IP1,
      col_Dns1,
      col_IP2,
      col_Dns2,
      counted
FROM temp
Union ALL
Select  col_IP1,
      col_Dns1,
      col_IP2,
      col_Dns2,
      counted
FROM result
) x
GROUP BY col_IP1,
       col_Dns1,
       col_IP2,
       col_Dns2
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

747 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

10 Experts available now in Live!

Get 1:1 Help Now