The CASE expression will allow you to do what you want.
For your debit/credit query, something along the lines of:
Code:
SELECT
CASE WHEN balance>0 THEN balance ELSE NULL END as Debit,
CASE WHEN balance<0 THEN balance END as Credit
...
The CASE expression will evaluate each condition and return as its result the first one which evaluates to TRUE. If no conditions are TRUE, CASE returns NULL. Thus, the two CASE expressions above are equivalent; one explicitly has an ELSE clause to handle situations where no WHEN clause evaluates to TRUE, the second does the same thing implicitly.
Your second example:
Code:
SELECT
CASE WHEN columnI=0 THEN ColumnJ ELSE columnK END as somecolumn,
...
Note that 'ColumnJ' is a scalar expression, so it could be just about anything, including a subquery referencing another table.
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com