SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old September 23rd, 2003, 04:15 AM
steveb1164 steveb1164 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 1 steveb1164 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
SQL Statement Help

I have 3 different tables with the same structure. Each row has an ID. Some of the IDs are the same in different tables. I need to add together the data from certain columns of different tables where the IDs are the same, but also include every ID that doesn't match in one of the other tables. For example:

Table 1:
ID, hits, walks
12 2 3
13 4 2
14 4 3

Table 2:
ID, hits, walks
12 3 5
15 2 2
16 1 3

Table 3:
ID, hits, walks
12 2 1
13 4 2
16 2 3

The SQL statement should return this:
ID, hits, walks
12 7 9
13 8 4
14 4 3
15 2 2
16 3 6

I'm going to put this data into an array on my webpage and use the array with ASP and VBscript to calculate other things and form my page. Maybe there is a better way to create this array using code instead of SQL. Can anyone help me out? Thanks.

Steve

Reply With Quote
  #2  
Old September 27th, 2003, 05:59 PM
dcarva's Avatar
dcarva dcarva is offline
Contributing User
ASP Free Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 633 dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 9 h 18 m 20 sec
Reputation Power: 7
Join your data together using an INNER JOIN. To get the data that doesn't match, use an OUTER JOIN. Once you join your data, you can add it using the SUM function in SQL. Hope this helps.

Reply With Quote
  #3  
Old October 7th, 2003, 10:04 AM
m_lazor m_lazor is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Luxembourg
Posts: 156 m_lazor User rank is Corporal (100 - 500 Reputation Level)m_lazor User rank is Corporal (100 - 500 Reputation Level)m_lazor User rank is Corporal (100 - 500 Reputation Level)m_lazor User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Didn't test the below query but I think you'll get the idea...

Sum(MySubQuery.hits) as hits, Sum(MySubQuery.walks) as walks
FROM
(SELECT Sum(hits) as hits, Sum(walks) as walks
FROM Table 1
GROUP BY ID

UNION

SELECT Sum(hits) as hits, Sum(walks) as walks
FROM Table 2
GROUP BY ID

UNION

SELECT Sum(hits) as hits, Sum(walks) as walks
FROM Table 3
GROUP BY ID) AS MySubQuery

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > SQL Statement Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT