Solved

SQL Query Results to new field based on criteria

Posted on 2009-07-07
8
248 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 142

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
 

Author Comment

by:ftarvin
ID: 24798646
worked like a charm!

Thanks for the quick info.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
query help 18 51
SQL query 4 29
Left Join with Tuple returning more rows 10 56
Insert with SET how to handle join 6 32
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now