SQL 在视图中使用交叉引用两个表的结果创建列

Posted

技术标签:

【中文标题】SQL 在视图中使用交叉引用两个表的结果创建列【英文标题】:SQL Creating a cloumn in a view with the results from cross-referencing two tables 【发布时间】:2017-07-17 09:42:16 【问题描述】:

我是 SQL 新手,正在尝试创建一个视图,该视图将来自读数数据库和故障数据库的数据结合起来。我想创建一个按目标名称排序的视图,然后是度量名称,然后是时间戳,并带有一个返回 1 的附加列,表示当天发生故障,否则返回零。我写的查询目前正在阅读我缺少右括号,但是当我消除括号时,它发现表名无效。我不确定我对案例的使用是否导致了它,尽管它已经在一些练习样本上起作用。任何检查此内容的帮助以及有关如何改进它的建议将不胜感激。

SELECT * FROM
(
with new_failure_table as  (
    SELECT target_name, END_TIMESTAMP,START_TIMESTAMP, 
    ((END_TIMESTAMP - (START_TIMESTAMP))*24*60) 
    FROM failure_table 
    WHERE (END_TIMESTAMP - (START_TIMESTAMP))*24*60 >5 
    AND  (END_TIMESTAMP - START_TIMESTAMP) < 1
    and availability_status = 'Target Down'
    ) 
    -- Simplifies failure table to include actual failures according to two parameters

SELECT
    t1.target_name,
    t1.metric_name,
    t1.rollup_timestamp,
    t1.average,
    t1.minimum,
    t1.maximum,
    t1.standard_deviation,
    t2.END_TIMESTAMP,
    t2.START_TIMESTAMP,
    (CASE  
    when t1.target_name = t2.target_name 
    and t1.rollup_timestamp = trunc(END_TIMESTAMP+1) 
    and t1.rollup_timestamp = trunc(START_TIMESTAMP+1) 
    THEN '1' ELSE '0' END) AS failure_status
    --Used to create column that reads 1 when there was a failure between the two readings and 0 otherwise    

FROM
    data_readings AS t1, new_failure_table AS t2 
WHERE t1.target_name = t2.target_name       
)

GROUP BY t1.target_name, metric_name
ORDER BY rollup_timestamp desc;

【问题讨论】:

【参考方案1】:

您不需要将case 括在括号中,而是可以

CASE  
when t1.target_name = t2.target_name 
and t1.rollup_timestamp = trunc(END_TIMESTAMP+1) 
and t1.rollup_timestamp = trunc(START_TIMESTAMP+1) 
THEN '1' ELSE '0' END AS failure_status

同样在FROM 中不要使用AS 代替:

...
FROM data_readings AS t1, new_failure_table AS t2 
...

使用

...
FROM data_readings t1, new_failure_table t2 
...

UPD:整个查询应该是这样的

SELECT * FROM
(
with new_failure_table as  (
    SELECT target_name, END_TIMESTAMP,START_TIMESTAMP, 
    ((END_TIMESTAMP - (START_TIMESTAMP))*24*60) 
    FROM failure_table 
    WHERE (END_TIMESTAMP - (START_TIMESTAMP))*24*60 >5 
    AND  (END_TIMESTAMP - START_TIMESTAMP) < 1
    and availability_status = 'Target Down'
    ) 
    -- Simplifies failure table to include actual failures according to two parameters

SELECT
    t1.target_name as target_name,
    t1.metric_name as metric_name,
    t1.rollup_timestamp as rollup_timestamp,
    t1.average,
    t1.minimum,
    t1.maximum,
    t1.standard_deviation,
    t2.END_TIMESTAMP,
    t2.START_TIMESTAMP,
    CASE  
    when t1.target_name = t2.target_name 
    and t1.rollup_timestamp = trunc(END_TIMESTAMP+1) 
    and t1.rollup_timestamp = trunc(START_TIMESTAMP+1) 
    THEN '1' ELSE '0' END AS failure_status
    --Used to create column that reads 1 when there was a failure between the two readings and 0 otherwise    

FROM
    data_readings t1, new_failure_table t2 
WHERE t1.target_name = t2.target_name       
)
GROUP BY target_name, metric_name
ORDER BY rollup_timestamp desc;

【讨论】:

嗨 Hatik,括号错误现在在第 28 行,我认为它就在 GROUP BY 函数的上方。我不确定,因为所有括号似乎都匹配。 @AlwaysInTheDark 尝试将group by 的括号移到order by 之后 奇怪的是,移动后出现相同的错误。如果它说缺少右括号,其他地方的函数是否需要括号? @AlwaysInTheDark 顺便说一句,您使用的是什么数据库引擎,是 Oracle 吗? 我使用的数据库引擎是Oracle

以上是关于SQL 在视图中使用交叉引用两个表的结果创建列的主要内容,如果未能解决你的问题,请参考以下文章

oracle 视图sql语句怎么写

在两个不同的表中创建具有相同列的视图 SQL

sql中修改了基本表的数据视图也会自动随之修改吗?

6.mysql视图

SQL中视图的创建。修改,删除

如何将两个表与 SQL Server 中第二个表中引用同一列的两列连接起来