SQL:将视图加入表以进行查询

Posted

技术标签:

【中文标题】SQL:将视图加入表以进行查询【英文标题】:SQL: joining view to table for query 【发布时间】:2017-03-27 05:24:50 【问题描述】:

SELECT January.customer_id as Jancust_id,
           SUM(payments.payment) as Jan_cust_pmts,
           COUNT(DISTINCT January.customer_id) AS Jan_orig_cust,
           COUNT(DISTINCT payments.customer_id) as Jan_ret_cust,
           AVG(payments.payment) as Cust_life_rev
           January.acquisition_source as Jan_source
    FROM January_Cohort January
    LEFT JOIN telemon_payments_data payments
    ON January.customer_id = payments.customer_id
    GROUP BY Jan_source

所以上面应该是一个查询,其中 January_Cohort 是一个已经创建的视图,我想将它加入到表 telemon_payments_data。

我是不是指错了,还是不能加入表格和视图? 错误消息是 January_Cohort 不是表;我知道,这是一种风景。

【问题讨论】:

【参考方案1】:

也许它会起作用 将视图设为子查询表

SELECT January.customer_id as Jancust_id,
           SUM(payments.payment) as Jan_cust_pmts,
           COUNT(DISTINCT January.customer_id) AS Jan_orig_cust,
           COUNT(DISTINCT payments.customer_id) as Jan_ret_cust,
           AVG(payments.payment) as Cust_life_rev
           January.acquisition_source as Jan_source
    FROM (select * from January_Cohort) January
    LEFT JOIN telemon_payments_data payments
    ON January.customer_id = payments.customer_id
    GROUP BY Jan_source

【讨论】:

以上是关于SQL:将视图加入表以进行查询的主要内容,如果未能解决你的问题,请参考以下文章

如何将 Access 查询迁移到 SQL 视图?

sql 曾经需要找到哪些表引用某个列?此SQL脚本将查询系统表以查找对g的所有引用

SQL:如何最好地查询历史表以创建特定日期的快照概览

根据用户输入查询 SQL 表以返回同一行的值

sql [sql] CTAS - 当您要创建临时表以执行进一步查询时使用。

基于 mysql 视图结果创建表以加快获取速度