Solved

SQL Query Results to new field based on criteria

Posted on 2009-07-07
8
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 60

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 60

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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

Author Comment

by:ftarvin
ID: 24798646
worked like a charm!

Thanks for the quick info.
0
 
LVL 60

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 60

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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