Query table value aliasing in Oracle SQL

Posted by Strata on Stack Overflow See other posts from Stack Overflow or by Strata
Published on 2011-02-20T04:36:53Z Indexed on 2011/02/20 7:25 UTC
Read the original article Hit count: 137

Filed under:
|
|
|
|

I have a homework assignment in SQL for Oracle 10g where I have to apply union to two different select statements, to return two columns. I need the values of each cell under vendor_state to indicate CA and every other value in another state to return "Outside CA", to indicate they're elsewhere.

I applied the union and produced the two columns and the listings for CA, but many other state IDs were listed and I couldn't find an explanation for how to change the actual values in the query itself. Eventually, I stumbled on an answer, but I can't explain why this works. The code is as follows:

  SELECT vendor_name, 
         vendor_state
    FROM vendors 
   WHERE vendor_state IN 'CA'
UNION
  SELECT vendor_name, 
         'Outside CA' AS vendor_state
    FROM vendors
   WHERE vendor_state NOT IN 'CA'
ORDER BY vendor_name

This gives me the exact answer I need, but I don't know why the aliasing in the second select statement can behave this way....no explanation is given in my textbook and nothing I've read indicates that column aliasing can be done like this. But, by switching the column name and the alias value, I have replaced the value being returned rather than the column name itself...I'm not complaining about the result, but it would help if I knew how I did it.

© Stack Overflow or respective owner

Related posts about sql

Related posts about Oracle