Solved

sql query with case statement and where clause

Posted on 2011-02-17
7
224 Views
Last Modified: 2012-05-11
I have a query that gets a few columns using case statements, and I then give the columns each an alias. I need to put in a 'where clause' using these columns, but if I use the alias, the columns are not recognized and I get an syntax error (Invalid Column Name). Can someone please tell me how to do this? I am including a simple example.

Thanks!
Amy
SELECT
CASE 
				WHEN CUSTOMER_ORDER.ADDR_NO >= 0
				THEN
					(SELECT TOP(1) CUST_ADDRESS.NAME FROM CUST_ADDRESS WHERE CUST_ADDRESS.ADDR_NO=CUSTOMER_ORDER.ADDR_NO AND CUST_ADDRESS.CUSTOMER_ID=CUSTOMER_ORDER.CUSTOMER_ID)
				ELSE
					CUSTOMER.NAME
				END
				AS CustName,

CASE 
				WHEN CUSTOMER_ORDER.ADDR_NO >= 0
				THEN
					(SELECT TOP(1) CUST_ADDRESS.CITY FROM CUST_ADDRESS WHERE CUST_ADDRESS.ADDR_NO=CUSTOMER_ORDER.ADDR_NO AND CUST_ADDRESS.CUSTOMER_ID=CUSTOMER_ORDER.CUSTOMER_ID)
				ELSE
					CUSTOMER.CITY				END
				AS CustCity

WHERE CustCity LIKE 'Jack%'

Open in new window

0
Comment
Question by:AmyL
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34920974

This does not seem to be complete. Is there more for this query
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34921076
you need to try like this.
select *
  from (
SELECT
CASE 
				WHEN CUSTOMER_ORDER.ADDR_NO >= 0
				THEN
					(SELECT TOP(1) CUST_ADDRESS.NAME FROM CUST_ADDRESS WHERE CUST_ADDRESS.ADDR_NO=CUSTOMER_ORDER.ADDR_NO AND CUST_ADDRESS.CUSTOMER_ID=CUSTOMER_ORDER.CUSTOMER_ID)
				ELSE
					CUSTOMER.NAME
				END
				AS CustName,

CASE 
				WHEN CUSTOMER_ORDER.ADDR_NO >= 0
				THEN
					(SELECT TOP(1) CUST_ADDRESS.CITY FROM CUST_ADDRESS WHERE CUST_ADDRESS.ADDR_NO=CUSTOMER_ORDER.ADDR_NO AND CUST_ADDRESS.CUSTOMER_ID=CUSTOMER_ORDER.CUSTOMER_ID)
				ELSE
					CUSTOMER.CITY				END
				AS CustCity
FROM Your_Table) t1
WHERE CustCity LIKE 'Jack%'

Open in new window

0
 

Author Comment

by:AmyL
ID: 34921103
Sorry, I forgot the join:

FROM CUSTOMER_ORDER, CUSTOMER
WHERE CUSTOMER_ORDER.CUST_ID=CUSTOMER.CUSTID AND CustCity LIKE 'Jack%'


...but this is not my actual query, just an example for concept.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AmyL
ID: 34921249
I get what you are saying...that makes sense. Unfortunately the syntax isn't working for me. Can you please create a simple example (just using any table names) for me?
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34921481
with CTE
as
(
      SELECT
      CASE
                              WHEN CUSTOMER_ORDER.ADDR_NO >= 0
                              THEN
                                    (SELECT TOP(1) CUST_ADDRESS.NAME FROM CUST_ADDRESS WHERE CUST_ADDRESS.ADDR_NO=CUSTOMER_ORDER.ADDR_NO AND CUST_ADDRESS.CUSTOMER_ID=CUSTOMER_ORDER.CUSTOMER_ID)
                              ELSE
                                    CUSTOMER.NAME
                              END
                              AS CustName,

      CASE
                              WHEN CUSTOMER_ORDER.ADDR_NO >= 0
                              THEN
                                    (SELECT TOP(1) CUST_ADDRESS.CITY FROM CUST_ADDRESS WHERE CUST_ADDRESS.ADDR_NO=CUSTOMER_ORDER.ADDR_NO AND CUST_ADDRESS.CUSTOMER_ID=CUSTOMER_ORDER.CUSTOMER_ID)
                              ELSE
                                    CUSTOMER.CITY                        END
                              AS CustCity
      FROM CUSTOMER_ORDER, CUSTOMER
      WHERE CUSTOMER_ORDER.CUST_ID=CUSTOMER.CUSTID
)
select * from CTE
WHERE CustCity LIKE 'Jack%'
0
 
LVL 41

Accepted Solution

by:
Sharath earned 250 total points
ID: 34921632
You need to warp the query in a CTE or a sub query.
select *
  from (
SELECT
      CASE 
                              WHEN CUSTOMER_ORDER.ADDR_NO >= 0
                              THEN
                                    (SELECT TOP(1) CUST_ADDRESS.NAME FROM CUST_ADDRESS WHERE CUST_ADDRESS.ADDR_NO=CUSTOMER_ORDER.ADDR_NO AND CUST_ADDRESS.CUSTOMER_ID=CUSTOMER_ORDER.CUSTOMER_ID)
                              ELSE
                                    CUSTOMER.NAME
                              END
                              AS CustName,

      CASE 
                              WHEN CUSTOMER_ORDER.ADDR_NO >= 0
                              THEN
                                    (SELECT TOP(1) CUST_ADDRESS.CITY FROM CUST_ADDRESS WHERE CUST_ADDRESS.ADDR_NO=CUSTOMER_ORDER.ADDR_NO AND CUST_ADDRESS.CUSTOMER_ID=CUSTOMER_ORDER.CUSTOMER_ID)
                              ELSE
                                    CUSTOMER.CITY                        END
                              AS CustCity
      FROM CUSTOMER_ORDER, CUSTOMER 
      WHERE CUSTOMER_ORDER.CUST_ID=CUSTOMER.CUSTID) t1
WHERE CustCity LIKE 'Jack%'

Open in new window

0
 

Author Closing Comment

by:AmyL
ID: 34922515
Perfect - thanks!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …

733 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