Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating "Conditional" Sums in Query 1

Status
Not open for further replies.

byrne1

Programmer
Aug 7, 2001
415
US
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.
 
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
 
Hi,

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

Regards,
Tom

 
Yeah, sorry I didn't respond soooner. This formula worked flawlessly! Thank you!
 
You're very welcome! And thanks for the star, much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top