我在Netezza工作 - 或者,你知道,分析的纯数据 - nzsql,但我认为这是一个ANSI SQL问题。问题是如此基本,我甚至不知道如何搜索它。
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',也就是说,查询可以很好地重用CLUNK别名,但PLONK别名会被源表中的列覆盖。现在,如果我真的想要源表中的列,我可以写SELECT SMORK AS PLONK,DEMO1.PLONK等,但我不知道如何指定我更喜欢我之前在同一个中定义的别名SELECT子句。
有人知道吗?
答案 0 :(得分: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
请注意 DESCRIPTION ,从表格列中挑选FULL_NAME值,而不是从别名中挑选。
如果您想要使用别名FULL_NAME中的DESCRIPTION列使用值,则可以分两步执行:
步骤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
答案 1 :(得分:0)
只需更改列的顺序即可。 Netezza尝试使用您的别名,因此您可以重命名列或更改顺序。
SELECT SMORK AS PLONK, PLONK, CLUNK, SPROING AS CLUNK
FROM DEMO1;