Solved

t-sql query help on case in where clause

Posted on 2011-03-10
13
533 Views
Last Modified: 2012-05-11
I have a t-sql query and can't make it work with the case in where:

select test_tb1.test_id,
          test_tb1.test_code,
          test_tb1. age_range,
         test_tb2.age
from test_tb1, test_tb2
where test_tb1.id = test_tb2.id
 and    test_tb1.test_code = 'o'
 and    test_tb1.age_range =
       case
           when test_tb2.age < 21 then '10-20'
           when test_tb2.age >=21 and test_tb2.age < 51 then '20-50'
           when test_tb2.age > 50 then '50+'
       end
0
Comment
Question by:jfreeman2010
13 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 35099797

use else for the last case option

select test_tb1.test_id, test_tb1.test_code, test_tb1. age_range, test_tb2.age
from test_tb1, test_tb2
where test_tb1.id = test_tb2.id
 and    test_tb1.test_code = 'o'
 and    test_tb1.age_range =
       case
           when test_tb2.age < 21 then
             '10-20'
           when test_tb2.age >=21 and test_tb2.age < 51 then
             '20-50'
           else
             '50+'
       end
0
 

Author Comment

by:jfreeman2010
ID: 35099914
Hi ewangoya,

thank you very much for the response.  It still not working after the medication...

I am using this query in coldfusion and also is a query of query ( both test_tb1 and test_tb2 are queries).
I am not sure may be this is a coldfusion and query of query problem.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35100263
could you please clarify "does not work" ...
any error messages?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35100455
I am using this query in coldfusion and also is a query of query ( both test_tb1 and test_tb2 are queries).

If you mean you're trying to use that SQL in a QoQ .. you can't.  When you run a QoQ's you're running an in memory query. Not a query against your db.  So you can only use the operators QoQ's support. They do not support CASE.

0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 35100651
Is there a reason you need a QoQ at all? They're not always as performant as you'd think and they're very rudimentary.  They don't support much beyond basic SELECT's.  IMO, you'd be better off doing the whole thing in a regular database query.  Then you'd have access to all of SQL Server 2008's features.

If you really *must* use a QoQ, use the CASE in your database query to add a calculated column called "AgeRangeValue".  Then use it in your QoQ:
     
       WHERE   test_tb1.age_range = test_tb2.AgeRangeValue
0
 

Author Comment

by:jfreeman2010
ID: 35100743
OK, if coldfusion not supporting case in QOQ. then I need to move the logic up to one level....   I am trying that now and have problem, here is the query.  THIS IS NOT A QoQ:

select test_tb1.test_id, test_tb1.test_code, test_tb1. age_range, test_tb1.initial
from test_tb1, test_tb2, test_tb3
where test_tb1.id = test_tb2.id
 and  test_tb1.p_id = test_tb3.p_id
 and   test_tb1.test_code = 'b'
      or
 (    test_tb1.test_code = 'o'
 and    test_tb1.age_range =
       case
           when test_tb2.age < 21 then
             '10-20'
           when test_tb2.age >=21 and test_tb2.age < 51 then
             '20-50'
           else
             '50+'
       end
  and
      test_tb1.initial =
        case when (


0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:jfreeman2010
ID: 35100772
Sorry, the last part of the case should be:

case when ( select count (pp_id)
                     from test_tb t
                      inner join test_tb5 t6
                       on t.id = t6.id ) > 1 then 'n'
           else  'y'
   end
           )
)


and I am getting error :
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

0
 

Author Comment

by:jfreeman2010
ID: 35100794
My first case this the about is working, but the second part of the case with count have error.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35100908
Well like the error message says, you can't use aggregates like COUNT(...) in that spot. Can you post the full sql?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35102060
select count (pp_id) from test_tb t inner join test_tb5 t6

Also what's the relationship between those 2 tables and the tables in the query?
0
 

Author Comment

by:jfreeman2010
ID: 35109391
Hi agx,

Thank you very much for helping, I full query are to much to post here, its a 8 tables join, I will try to work it out, and if still has problem, will come up a example in a hour to see if you can help...
0
 

Author Closing Comment

by:jfreeman2010
ID: 35109717
THANK YOU.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35110647
@jfreeman2010 - Did you get it working? I tried to re-write an example, but couldn't figure out how to relate the 2 tables in the subquery to the rest of the tables :)
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

862 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

24 Experts available now in Live!

Get 1:1 Help Now