Solved

putting IF logic into a calculated column?

Posted on 2010-08-17
7
259 Views
Last Modified: 2012-05-10
With the below query, is it possible to insert some logic which would output a text string into the next column depending on the value of the last column (which is calculated)

e.g. "small" if value <= 10, otherwise "large"

Or do we need to do some logic in TSQL for this?
i.e. open up a cursor, store the value of col#4 in a variable, do our logic on it, and output a string using PRINT depending on its value ?

cheers!
select EA.ex_pi_eval_answer_school_id, 

SUM(CAST(EA.ex_pi_eval_answer_response AS DECIMAL)), 

COUNT(EA.ex_pi_eval_answer_response),

SUM(CAST(EA.ex_pi_eval_answer_response AS DECIMAL)) / COUNT(EA.ex_pi_eval_answer_response)



from tbl_ex_ProgramImpact_eval_answers EA



where EA.ex_pi_eval_answer_ProgramObjective_id = 1



group by EA.ex_pi_eval_answer_school_id

Open in new window

0
Comment
Question by:zorba111
  • 4
  • 2
7 Comments
 

Author Comment

by:zorba111
Comment Utility
NB typical output from the current query is like:

2036449      19      5      3.8
4010885      22      10      2.2
1116159      23      5      4.6
2046646      23      5      4.6
4036081      23      5      4.6
1016647      22      5      4.4
2036149      21      5      4.2
4230165      20      5      4

(excel, which I've used as an intermediary to copy this data, has stripped out non-significant trailing zeros)
0
 
LVL 16

Accepted Solution

by:
vdr1620 earned 400 total points
Comment Utility
you can use a CASE Statement Like

OUTPUTColumn = CASE WHEN Value<=10 THEN 'Small' ELSE 'Large' END

The below blog has some good explanation with examples
http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/
0
 

Author Comment

by:zorba111
Comment Utility
@vdr1620

Hi this is great, however having read up on CASE WHEN, I fear it will not work.

I had simplified my problem somewhat, so its not your fault!

There are actually three ranges (NOT two) that the values could fall into, and for each value, a different string gets output :-(
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 16

Assisted Solution

by:vdr1620
vdr1620 earned 400 total points
Comment Utility
Not a problem..you can use

CASE WHEN (condition) THEN Statement
          WHEN (condition) THEN Statement
           WHEN (condition) THEN Statement END

OUTPUTColumn = CASE WHEN Value<=10 THEN 'Small'
                                        WHEN Value >10 THEN 'Large'
                                        WHEN Value >100 THEN 'SomeotherValue'        END

0
 

Author Comment

by:zorba111
Comment Utility
@vdr1620

hmm, having looked at Pinal Dave's page (the link you kindly referred me to), I think now that all may not be lost!

I will report back....
0
 

Author Comment

by:zorba111
Comment Utility
ok, it wasn't in his blog, but in MSDN I found this usage....

SELECT    'Price Category' =
      CASE
         WHEN price IS NULL THEN 'Not yet priced'
         WHEN price < 10 THEN 'Very Reasonable Title'
         WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
         ELSE 'Expensive book!'
      END,
   CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 100 total points
Comment Utility
You can also do something like this, if you need to:

SELECT
    CASE WHEN price IS NULL THEN '...'
              WHEN price < 10 THEN '...'
              WHEN price >= 10 AND price < 20 THEN '...'
              ELSE '...' END +
    ',' +
    CASE
              WHEN size < 10 THEN 'Small'
              WHEN size > 10 AND size < 20 THEN 'Medium'
              WHEN size >= 20 THEN 'Large'
              ELSE 'Unknown size' END
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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