[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

sql query with case statement and where clause

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
AmyL
Asked:
AmyL
  • 3
  • 2
  • 2
1 Solution
 
Ephraim WangoyaCommented:

This does not seem to be complete. Is there more for this query
0
 
SharathData EngineerCommented:
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
 
AmyLAuthor Commented:
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
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!

 
AmyLAuthor Commented:
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
 
Ephraim WangoyaCommented:
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
 
SharathData EngineerCommented:
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
 
AmyLAuthor Commented:
Perfect - thanks!
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now