2014-06-16 43 views
1

我正在Netezza工作 - 或者,您知道Google Analytics的純數據--nzsql,但我認爲這是一個ANSI SQL問題。這個問題非常基本,我甚至不知道如何搜索它。在查詢中的字段別名化nzsql

CREATE TEMPORARY TABLE DEMO1 AS SELECT 'SMORK' AS SMORK, 'PLONK' AS PLONK, 'SPROING' AS SPROING; 

SELECT SMORK AS PLONK, PLONK, SPROING AS CLUNK, CLUNK 
FROM DEMO1; 

這將返回「SMORK,plonk的,SPROING,SPROING」,這是說,查詢是好的重用悶響別名,但plonk的別名是通過從源表中的列覆蓋。現在,如果我真的想要源表中的列,我可以編寫SELECT SMORK AS PLONK,DEMO1.PLONK et c,但我不知道如何指定我更喜歡早先定義的別名, SELECT子句。

有沒有人知道一種方式?

回答

1

在Netezza公司,選擇列時,Netezza公司將搜索表的列,然後再別名。

例子: 假設我們有以下語句:

CREATE TEMPORARY TABLE EMPLOYEES AS 
SELECT 1001 AS EMPLOYEE_ID 
     ,'Alex' AS FIRST_NAME 
     ,'Smith' AS LAST_NAME 
     ,'Alex J. Smith' AS FULL_NAME; 

SELECT 
    EMPLOYEE_ID 
    ,FIRST_NAME 
    ,LAST_NAME 
    ,LAST_NAME||', '||FIRST_NAME AS FULL_NAME 
    ,'My full name is :'||FULL_NAME AS DESCRIPTION 
    FROM EMPLOYEES; 

它將返回

 
EMPLOYEE_ID FIRST_NAME LAST_NAME FULL_NAME  DESCRIPTION 
    1001  Alex  Smith  Smith, Alex My full name is :Alex J. Smith 

注意在描述,則FULL_NAME值從表列採摘,而不是別名。

如果你想從別名FULL_NAME描述列使用價值,你可以做的兩個步驟:

第1步:創建一個子查詢包括你想要的所有列。對於要重用的所有別名,需要將它們命名爲FROM子句中任何表列中不存在的名稱;

第2步。僅從子查詢中選擇需要的列。

CREATE TEMPORARY TABLE EMPLOYEES AS SELECT 1001 AS EMPLOYEE_ID, 'Alex' AS FIRST_NAME, 'Smith' AS LAST_NAME, 'Alex J. Smith' AS FULL_NAME; 

WITH EMPLOYESS_TMP AS (
SELECT 
    EMPLOYEE_ID 
    ,FIRST_NAME 
    ,LAST_NAME 
    ,LAST_NAME||', '||FIRST_NAME AS FULL_NAME2 
    ,FULL_NAME2 AS FULL_NAME 
    ,'My full name is :'||FULL_NAME2 AS DESCRIPTION 
    FROM EMPLOYEES) 
SELECT 
    EMPLOYEE_ID 
    ,FIRST_NAME 
    ,LAST_NAME 
    ,FULL_NAME 
    ,DESCRIPTION 
FROM EMPLOYESS_TMP; 

這將返回你想要什麼:

 
EMPLOYEE_ID FIRST_NAME LAST_NAME FULL_NAME  DESCRIPTION 
    1001  Alex  Smith Smith, Alex  My full name is :Smith, Alex 
0

只需更改列的順序。 Netezza會嘗試使用您的別名,以便您可以重命名列或更改順序。

SELECT SMORK AS PLONK, PLONK, CLUNK, SPROING AS CLUNK 
FROM DEMO1;