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 August 10th, 2004, 01:29 PM
Schadrach Schadrach is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 55 Schadrach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 17 m 53 sec
Reputation Power: 5
Totalling higher of two columns by row.

Assume that I have a table containing two fields, Total and Total2. I need the sum of whichever is higher for each row, for example, with the following sample data:

Code:
Total | Total2
   1   |   3
   2   |   1
   5   |   10


The query should return 15(3+2+10).

Any good ideas on how to do this simply?

Reply With Quote
  #2  
Old August 10th, 2004, 01:44 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
1) That isn't good database design.
2) In order to return the information you need, you would need to write a Case statement to determine which value in each row is higher, then add those together.

Reply With Quote
  #3  
Old August 10th, 2004, 02:33 PM
Schadrach Schadrach is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 55 Schadrach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 17 m 53 sec
Reputation Power: 5
I know it isn't exactly good database design, but those fields are part of the results of another query, each containing the sum of a column on another table, generated off of some much more meaningful data. Generally the second total is the one to use, but there are some old legacy entries in the table from before the second total existed, and they need to be accounted for.

I've been working on the system this is part of for a while, and I keep getting requests for additional features to be added to the system, which need to be wedged in via the most efficient route available. This means that some parts of the system are not exactly the most attractive or well designed pieces of code, since they were originally intended to do about half of the things they currently do.

Frankly, if I knew it had to do what it does now (nm what it will do in a week), I would have designed it completely differently from the beginning (Hell, the DB would even be completely in 4th normal form, but needing to arbitrarily pull fields/tables in and out because the people you're coding for change their requirements daily makes it less reasonable to guarantee things stay that way).

In fact, ATM I'm revising some display pages and throwing out a lot of old, very ugly, very kludgy code. Was hoping there was a more elegant way to do that total than to loop through the recordset and add it up as I went, but apparently that's as clean as it can get.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Totalling higher of two columns by row.


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


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





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