×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Creating "Conditional" Sums in Query

Creating "Conditional" Sums in Query

Creating "Conditional" Sums in Query

(OP)
I have a table that has the following fields:

<customernum>
<transtype>
<tickettotal>

I want to get a sum of sales grouped by <customernum>.  Should be simple enough, except that the <tickettotal> field is always stored as a positive value, even for credits (as indicated by <transtype>="CRD").  What I want is the total sum of sales minus the sum of credits by customer.

Is there a way I can put this in a SQL query and have my server do the work, or do I have to do this on the client end?

BTW, I'm running Pervasive.SQL 8.6.

RE: Creating "Conditional" Sums in Query

Hi,

You could try using the IF statement with your select.  The IF command works a bit like the CASE command in SQL Server.

SELECT
SUM(IF (TransType = 'CRD',
TicketTotal * -1,
TicketTotal))
FROM <NameofyourTable>

Hope this helps,
Tom

RE: Creating "Conditional" Sums in Query

Hi,

I was wondering if yuo had any sucess with your issue?

Regards,
Tom

RE: Creating "Conditional" Sums in Query

(OP)
Yeah, sorry I didn't respond soooner.  This formula worked flawlessly!  Thank you!

RE: Creating "Conditional" Sums in Query

You're very welcome!  And thanks for the star, much appreciated.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close