Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL syntax for calculations

Posted on 1997-10-10
5
Medium Priority
?
183 Views
Last Modified: 2010-05-18
What is the syntax for creating conditional calculations?
For example:
  select sum(if (Field1 = 'x') then 8 else 1)
  from Table1

I'm accustomed to Oracle SQL which would format the above as:
  select sum(decode(Field1, 'x', 8, 1))
  from Table1
0
Comment
Question by:EBuffey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1347234
Hi EBuffy,
Don't think is possible.
Believe you can only use Sum(ALL Field1) or Sum(DISTINCT Field1).
I think you better make calculated fields where in you make your
conditions. Then you Sum these calculated fields.
Have fun,
c.u. ZifNab;
0
 
LVL 3

Expert Comment

by:Pegasus100397
ID: 1347235
EBuffey,
I'm not sure what your example statement is trying to accomplish (in real life) but I'll do my best. As I do not know what database you are *wanting* to use, I'll assume MS SQL Server

Select Answer =
   Case
     WHEN Field1 = 'x' then 8
    else 1
from Table1

The previous example is more commonaly used when a field needs to be calculated and checked against a result such as

...
WHEN (Field1 * 30 < 400) then blah, blah blah
...

If you have a condition simply based on the value of a field (such as Yes or No) then the following would be more appropriate

Select Answer =
   Case Field1
            WHEN 'Yes' then 'YesAnswer'
            WHEN 'No' then 'NoAnswer'
           else 'Bad_Value_In_Field1'
from Table1

Note that the value following the THEN statement need not be a simple constant. It could be another select statement, another Case statement, etc. Some of my queries (for financial purposes) run in excess of 12 pages with many, many levels of conditional statements.

If prefer the case statement for my conditional SQL as opposed to the IF statement. Personal preference :)

Hope this helps, if not... hey! Can't blame a guy for trying!

tnorris
0
 

Author Comment

by:EBuffey
ID: 1347236
Pegasus's answer looked great but it didn't work! The reason is that I'm using local SQL to access a Paradox table. (i.e. from Database Desktop)

I got the infamous "Invalid use of keyword" message! Which (if Borland is listening) is an absolutely USELESS error message when there is no examples in the help files nor in the Borland manuals that come with Delphi 2.0!!!
0
 

Author Comment

by:EBuffey
ID: 1347237
Since my "real life" query uses a numeric field, I was able to solve my own problem with the following SQL! However, I'd still like to know if there is a way to do this using an alpha field!

  select sum((Field1 / 64) * 7 + 1) as DesiredTotal
  from Table1

The above query will use a value of 1 when Field1 < 64 and use a value of 8 when Field1 >= 64.
0
 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 100 total points
ID: 1347238
EBuffey,

Sorry my previous answer didn't work. I don't work with Local SQL much but your question intriegued me so I went and had a look.

Your answer is: It cannot be done in Local SQL because this "version" of SQL does not contain any coditional select statements. My recommendation would be to get as close as you can with the simplistic SQL they give you and tdo the rest in your program.

Sorry,
Tracy

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

609 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