Solved

sql query with case statement and where clause

Posted on 2011-02-17
7
226 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

Technology Partners: 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!

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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