Tableau Oracle DB SQL列名称选择空格

时间:2017-09-08 20:32:36

标签: sql oracle tableau

我在Tableau中使用自定义SQL查询,该查询连接到Oracle DB中的表。我有一些带有空格的列名,有些没有空格。如果我使用带有空格的列名,我会收到错误。请参阅以下内容:

Column Names
"Department"
"Job Type"

用于列名称无空格的方法

SELECT Department FROM OG_COCKPIT_HC_CONTR_V -- (WORKS!)

SELECT (Department) FROM OG_COCKPIT_HC_CONTR_V -- (WORKS!)

SELECT "Department" FROM OG_COCKPIT_HC_CONTR_V -- (DOESN'T WORK)

SELECT [Department] FROM OG_COCKPIT_HC_CONTR_V -- (DOESN'T WORK)

SELECT 'Department' FROM OG_COCKPIT_HC_CONTR_V -- (DOESN'T WORK)

对于"工作类型"领域没有上述作品。你知道什么是正确的语法吗?

2 个答案:

答案 0 :(得分:2)

select id,sum(if(purchase_type='A',1,0)) as sumA,sum(if(purchase_type='B',1,0)) as sumB 
from purchases
group by id
having (sumA>0 and sumB=0) OR(sumA=0 and sumB>0 )

应该可以正常工作,但要记住区分大小写。

泰德。

答案 1 :(得分:1)

如果SELECT Department ...有效,但SELECT "Department" ...不起作用,则意味着列部门的定义没有双引号且不区分大小写。以下所有陈述都可以正常使用:

select Department, "Job Type" FROM og_cockpit_hc_contr_v;
select department, "Job Type" FROM og_cockpit_hc_contr_v;
select DEPARTMENT, "Job Type" FROM og_cockpit_hc_contr_v;

工作类型可能是拼写错误?请考虑以下示例:

SQL>     create or replace view og_cockpit_hc_contr_v as 
         select 'Dep1' as Department, 'Dummy' as "Job Type" from dual
         ;

SQL>     select column_name from user_tab_cols 
         where table_name= 'OG_COCKPIT_HC_CONTR_V'
         and (upper(column_name) like 'JOB%' or upper(column_name) like'DEP%')
         ;

COLUMN_NAME
-----------------
DEPARTMENT
Job Type

SQL>     col department for a10
SQL>     col "Job Type" for a10
SQL>     select Department, "Job Type" FROM og_cockpit_hc_contr_v;

DEPARTMENT Job Type
---------- ----------
Dep1       Dummy

Note: Oracle建议不要对数据库对象名称使用带引号的标识符。 SQL * Plus接受这些带引号的标识符,但在使用管理数据库对象的其他工具时它们可能无效。