If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Sum function with logic?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-29-07, 07:31
wallaceoc wallaceoc is offline
Registered User
 
Join Date: Oct 2007
Posts: 14
Sum function with logic?

Hi,

I want to use the sum function to get the sum of a * b where a is always the value of the "face" column but b can be the value from one of 2 columns dependent on a certain condition. So simply my SQL would look like this if b was a fixed column:

select sum(a*b)
from tableA, tableB....

What is the best way to add logic to this. Basically, below is what I want to do:

select sum(a * [if tableA.code = 1 then use tableA.rate Else use tableB.rate])
from tableA, tableB


I hope I have explained this properly.

Regards,
Wallace
Reply With Quote
  #2 (permalink)  
Old 10-29-07, 07:42
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Does your SQL implementation support the CASE statement? If it does, that is your answer.

Beware the cartesian join. Those can be sub-optimal.

-PatP
Reply With Quote
  #3 (permalink)  
Old 10-29-07, 09:34
wallaceoc wallaceoc is offline
Registered User
 
Join Date: Oct 2007
Posts: 14
Yes the CASE statement did the trick.

Thanks for the quick reply.

Wallace
Reply With Quote
  #4 (permalink)  
Old 10-31-07, 06:56
wallaceoc wallaceoc is offline
Registered User
 
Join Date: Oct 2007
Posts: 14
I have one last question about the CASE statement and can't find the answer anywhere...

Is it possible to specify multiple conditions in the WHEN part of the statement?

Something like this:

select.....
CASE type
WHEN 6 OR 7 OR 8 THEN Rate1
ELSE
Rate2
......
END


Thanks
Reply With Quote
  #5 (permalink)  
Old 10-31-07, 08:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, but using the other form of the CASE expression
Code:
CASE WHEN type IN ( 6 , 7 , 8 )
     THEN Rate1
     ELSE Rate2
 END
the form that you were using would look like this
Code:
CASE type
  WHEN 6 THEN Rate1
  WHEN 7 THEN Rate1
  WHEN 8 THEN Rate1
         ELSE Rate2
 END
and i prefer the former
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 10-31-07, 08:18
wallaceoc wallaceoc is offline
Registered User
 
Join Date: Oct 2007
Posts: 14
Thanks again.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On