Solved

sql query with case statement and where clause

Posted on 2011-02-17
7
219 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility

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

Expert Comment

by:Sharath
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:AmyL
Comment Utility
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:ewangoya
Comment Utility
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 40

Accepted Solution

by:
Sharath earned 250 total points
Comment Utility
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
Comment Utility
Perfect - thanks!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel to create SQL insert 7 34
IF SQL Statement Access SQL 6 44
query Syntax 4 32
SQL Round a percentage 2 23
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

6 Experts available now in Live!

Get 1:1 Help Now