?
Solved

sql query with case statement and where clause

Posted on 2011-02-17
7
Medium Priority
?
227 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 1000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

752 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