Unix Technical Forum

Weigh resultset

This is a discussion on Weigh resultset within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, This is what I want to do. For a data acquisition query, if condition A is met, set ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 05:25 PM
Doug Baroter
 
Posts: n/a
Default Weigh resultset

Hi,

This is what I want to do. For a data acquisition query, if condition
A is met, set weight to 1, else if condition B is met, set weight to 2
etc.
Is this possible? I thought about using CASE function but to no
avail.

Using the classical Northwind db as target db, and its employees table
as target table, if [note] column contains 'BA', then I want to set
weight to 1, else if it contains 'BTS', then I want to set weight to 2
etc. Weight or the like is an auxillary artificially created column.

Something like

DECLARE @col int

select firstname, lastname, @col =
(
CASE notes
when '%BA%' then 1
when '%BTS%' then 2
else 0
END as aiCOL
)
-- and I'd like to alias @col as aiCOL or whatever
from employees
where notes LIKE
'%BA%'
OR
notes LIKE '%BTS%'

failed.

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 05:25 PM
Doug Baroter
 
Posts: n/a
Default Re: Weigh resultset

OK, so, I don't need the var, thanks.

"Anith Sen" <anith@bizdatasolutions.com> wrote in message news:<faTYa.90456$3o3.6253866@bgtnsc05-news.ops.worldnet.att.net>...
> Your CASE expression syntax should be search-based WHEN clause like :
>
> SELECT fname, lname,
> CASE WHEN notes LIKE '%ba% THEN 1
> WHEN notes LIKE '%bt% THEN 2
> ELSE 0
> END
> FROM Employees
> WHERE ..
>
> Also, you cannot do variable assignment & data retrieval together in a
> single SELECT statement. If you need to get the return result of the CASE
> expression into a variable @col, then you have to remove the other columns
> in the SELECT list.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 05:25 PM
Steve Kass
 
Posts: n/a
Default Re: Weigh resultset

Doug,

You've got some simple syntax errors. Does this help?

select
firstname,
lastname,
case
when notes like '%BA%' then 1
when notes like '%BTS%' then 2
else 0 end as aiCOL
from Northwind..Employees
where notes LIKE '%BA%' or notes LIKE '%BTS%'

-- Steve Kass
-- Drew University
-- Ref: 5CAD7D03-5761-4ECC-8E43-82E2A0F67529

Doug Baroter wrote:

>Hi,
>
>This is what I want to do. For a data acquisition query, if condition
>A is met, set weight to 1, else if condition B is met, set weight to 2
>etc.
>Is this possible? I thought about using CASE function but to no
>avail.
>
>Using the classical Northwind db as target db, and its employees table
>as target table, if [note] column contains 'BA', then I want to set
>weight to 1, else if it contains 'BTS', then I want to set weight to 2
>etc. Weight or the like is an auxillary artificially created column.
>
>Something like
>
>DECLARE @col int
>
>select firstname, lastname, @col =
> (
> CASE notes
> when '%BA%' then 1
> when '%BTS%' then 2
> else 0
> END as aiCOL
> )
> -- and I'd like to alias @col as aiCOL or whatever
>from employees
>where notes LIKE
> '%BA%'
> OR
> notes LIKE '%BTS%'
>
>failed.
>
>Thanks.
>
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 05:26 PM
Doug Baroter
 
Posts: n/a
Default Re: Weigh resultset

Thanks, Steve, I got it from the first responder, please check out a
new question just posted entitled "Tough problem".

Steve Kass <skass@drew.edu> wrote in message news:<bh2041$djn$1@slb4.atl.mindspring.net>...
> Doug,
>
> You've got some simple syntax errors. Does this help?
>
> select
> firstname,
> lastname,
> case
> when notes like '%BA%' then 1
> when notes like '%BTS%' then 2
> else 0 end as aiCOL
> from Northwind..Employees
> where notes LIKE '%BA%' or notes LIKE '%BTS%'
>
> -- Steve Kass
> -- Drew University
> -- Ref: 5CAD7D03-5761-4ECC-8E43-82E2A0F67529
>
> Doug Baroter wrote:
>
> >Hi,
> >
> >This is what I want to do. For a data acquisition query, if condition
> >A is met, set weight to 1, else if condition B is met, set weight to 2
> >etc.
> >Is this possible? I thought about using CASE function but to no
> >avail.
> >
> >Using the classical Northwind db as target db, and its employees table
> >as target table, if [note] column contains 'BA', then I want to set
> >weight to 1, else if it contains 'BTS', then I want to set weight to 2
> >etc. Weight or the like is an auxillary artificially created column.
> >
> >Something like
> >
> >DECLARE @col int
> >
> >select firstname, lastname, @col =
> > (
> > CASE notes
> > when '%BA%' then 1
> > when '%BTS%' then 2
> > else 0
> > END as aiCOL
> > )
> > -- and I'd like to alias @col as aiCOL or whatever
> >from employees
> >where notes LIKE
> > '%BA%'
> > OR
> > notes LIKE '%BTS%'
> >
> >failed.
> >
> >Thanks.
> >
> >

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:08 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com