Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Compute a field in select with a WHERE

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-11-12 : 08:07:15
Jennifer writes "I need to beable to compute two new fields in a select statement based on a value in another field. Say I have a field called ACCTTYPE that has values 'DEBIT' or 'CREDIT' and I have another field called AMOUNT. I need to establish two new fields DEBITAMTS and CREDITAMTS. Also, I would like a total at the end (I don't know how to generate a total) this is kind of what I figured:

SELECT ACCTTYPE, AMOUNT,
(AMOUNT where ACCTTYPE = 'DEBIT') AS 'DEBITAMTS',
(AMOUNT where ACCTTYPE = 'CREDIT') AS 'CREDITAMTS'
FROM ACCTINGTABLE TOTAL AMOUNT, DEBITAMTS, CREDITAMTS

yields something like:

ACCTTYPE AMOUNT DEBITAMTS CREDITAMTS
DEBIT 50.00 50.00 0.0
CREDIT 75.00 0.00 75.00

total 125.00 50.00 75.00"

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2004-11-12 : 09:13:22
investigate the CASE construct.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-11-12 : 09:25:50
the total is typically done at the presentation layer. SQL is used to retrieve the data; somehow you must use something else to display it to the user (i.e., Excel or Crystal Reports or Access or a Web Page). It is almost always much easier and more efficeint to handle totalling at the presentation layer.

- Jeff
Go to Top of Page
   

- Advertisement -