Solved

SQL Query Results to new field based on criteria

Posted on 2009-07-07
8
252 Views
Last Modified: 2012-05-07
select d as dog, b as boy, 1 as [positive], 1 as [negative]
 where x

so in this example the result in positive and negative would be the same.

I need to add logic to query so that if query result is positive number then result will only show up in [positive] else if amount is negative it will only show up in new [negative] field in result set.
0
Comment
Question by:ftarvin
  • 4
  • 3
8 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 24798175
For example if x is your value, then you can do like this:
select d as dog, b as boy, case when x >= 0 then x end as [positive], case when x < 0 then x end as [negative]

Open in new window

0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24798177
you need somme CASE expression
select dog, boy, value
, case when value > 0 then value end as positive
, case when value < 0 then value end as negative
from (
select d as dog, b as boy
  , (some_expression) as value 
 from sometable
 where x
) as sub_query

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24798203
For MySQL you can also use the IF construct:
SELECT IF(x>=0,x,NULL) AS [positive]
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:ftarvin
ID: 24798646
worked like a charm!

Thanks for the quick info.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24798658
ftarvin,

You are most welcome.

Happy coding!

Best regards,
Kevin
0
 

Author Comment

by:ftarvin
ID: 24798661
both experts provided same solution at the same time... split points?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24798675
Definitely appropriate. :)
0
 

Author Closing Comment

by:ftarvin
ID: 31600808
Thanks again boyz.. funny thing is I had tried that but missed a #$% comma from previous select field and kept thinking it was an issue with my case expression! sloppy!

Speak to you soon Hall of Famer's !
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question