Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 writing a simple join with sum function
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

4 Posts

Posted - 04/30/2013 :  17:18:30  Show Profile  Reply with Quote
Hello All

i have these to tables

country: name,population
borders: country1,country2
every country have some neghbors, in borders table,
now i should calculate sum of pupulation of all neighbors of every country
thank u so much for ur help
I use oracle 11g

i wrote this but im sure that its pretty incorrect

from country co, borders bo, country co2


Aged Yak Warrior

549 Posts

Posted - 04/30/2013 :  22:34:16  Show Profile  Reply with Quote
Here is a solution to get what you need using Microsoft SQL queries. (BTW this is MSSQL Forum)
The actual query might work on Oracle 11g as well. (I don't have Oracle installed to test this query)

if OBJECt_ID('dbo.Country', 'U') IS NOT NULL DROP TABLE dbo.Country;
CREATE TABLE [dbo].[Country](
Name NVARCHAR(20),
PopulationNum INT);

if OBJECt_ID('dbo.Borders', 'U') IS NOT NULL DROP TABLE dbo.Borders;
CREATE TABLE [dbo].[Borders](
Country1 NVARCHAR(20),
Country2 NVARCHAR(20));

INSERT INTO Country(Name, PopulationNum) VALUES 
('China', 2004500),
('Pakistan', 500220),
('Afghanistan', 60000),
('USA', 400000),
('Canada', 100034),
('Mexico', 506002),
('Bangladesh', 10012),
('India', 1000657);

INSERT INTO Borders(Country1, Country2)
VALUES ('India', 'Pakistan'),
('China', 'Pakistan'),
('India', 'Bangladesh'),
('Afghanistan', 'Pakistan'),
('USA', 'Canada'),
('USA', 'Mexico');


SELECT F.CountryOfInterest, SUM(F.PopulationNum) as NeighborTotal from  
(SELECT CB.CountryOfInterest, CB.BorderCountry, C.PopulationNum from 
	(SELECT Country1 as CountryOfInterest, Country2 as BorderCountry from Borders
	SELECT Country2 as CountryOfInterest, Country1 as BorderCountry from Borders) as CB, 
Country C where CB.BorderCountry = C.Name) as F 
group by F.CountryOfInterest;

Go to Top of Page

Aged Yak Warrior

549 Posts

Posted - 05/01/2013 :  10:42:45  Show Profile  Reply with Quote
If you have symmetric data as you described in
The following query should work.

SELECT CB.Country1, SUM(C.Population) as NeighborTotal from  
Borders as CB, Country C where CB.Country2 = C.Name
group by CB.Country1;

The query I posted earlier assumes asymmetric data (with no duplicate information).
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000