Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 13th, 2003, 07:14 AM
Authorized User
 
Join Date: Nov 2003
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dispatching data between columns of a query (wow!)


Hi,

Is there a way to :

(a) take data from the same column of a table and put it in various columns of a query, based on some criteria you specify

Example :

Query has 2 columns, "Debit" and "Credit". If account balance in querried table is > 0, write value in column "Debit", if not, write in column "Credit"

(b) take data from several table columns and put it in the same column of the query, based on some criteria

Example :

If column "I" of a query yields 0 for the current line of the query, then look in column "J" of same table (or different ?) and read that one

I feel that this is similar to an IIF function or something.

It might be that these are elementary questions, but can U help me ?
Thanx

Mike




 
Old December 13th, 2003, 07:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Query to Convert Columns into Rows Niaz Oracle 3 February 5th, 2010 07:08 AM
Moving Columns over on the fly with delete query kevorkian SQL Server ASP 0 March 17th, 2006 12:47 PM
getting a query rows as columns raamts SQL Language 1 February 24th, 2005 04:06 AM
SQL Query to Convert Columns into Rows Niaz SQL Server 2000 2 April 20th, 2004 01:36 AM
total columns in sql query nlicata SQL Server ASP 1 August 4th, 2003 06:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.