2017-04-26 41 views
0

如何使用语句创建视图? 我得到的错误就可以了:使用语句创建视图

WITH temp as (
select uu.email, u.logintime, u.region, p.id as panelid, p.panelname, p.numberofdownloads, dimensionType + ' (' + dimensionValue + ')' as filter 
from stat_users u 
left join stat_panels p 
on u.id=p.sessionid 
left join stat_filters f 
on p.id=f.panelid 
left join users uu 
on uu.id=u.userid 
where uu.Organization = 'name' AND 
    year(logintime) between 2015 and 2017 
    and panelname is not null 
) 


CREATE VIEW final as(
    select aa.email, aa.logintime, aa.region, aa.panelname, aa.numberofdownloads as downloads, case when len(aa.filters) > 0 then left(aa.filters, len(aa.filters)-1) else '' end as filters 
    from (
    Select distinct a.email, a.logintime, a.region, a.panelname, a.numberofdownloads, 
       (
        Select b.filter + ', ' AS [text()] 
        From temp b 
        Where b.panelid=a.panelid 
        ORDER BY b.panelid 
        For XML PATH ('') 
       ) filters 
    from temp a 
    ) aa 

) 我得到这样的错误:

> Incorrect syntax near the keyword 'CREATE'. 'CREATE VIEW' must be the 
> first statement in a query batch. 

所以,我只需要使用创建使用选择视图,其基于WITH声明SQL SERVER 2014

+0

而且我得到一个错误,如果我把最初建立一个观点: – mondayguy

回答

1

是总是CREATE必须是查询批次中的第一条语句

CREATE VIEW vFinal AS 
WITH Temp AS (
SELECT uu.email, u.logintime, u.region, p.id AS panelid, p.panelname, p.numberofdownloads, dimensionType + ' (' + dimensionValue + ')' AS Filter 
FROM stat_users u 
LEFT JOIN stat_panels p ON u.id=p.sessionid 
LEFT JOIN stat_filters f ON p.id=f.panelid 
LEFT JOIN users uu ON uu.id=u.userid 
WHERE uu.Organization = 'name' AND 
         YEAR(logintime) BETWEEN 2015 AND 2017 
         AND panelname IS NOT NULL 
) 
SELECT aa.email, aa.logintime, aa.region, aa.panelname, aa.numberofdownloads AS downloads, CASE WHEN LEN(aa.filters) > 0 THEN LEFT(aa.filters, LEN(aa.filters)-1) else '' end as filters 
    FROM (
    SELECT DISTINCT a.email, a.logintime, a.region, a.panelname, a.numberofdownloads, 
       (
        SELECT b.filter + ', ' AS [text()] 
        FROM temp b 
        WHERE b.panelid=a.panelid 
        ORDER BY b.panelid 
        FOR XML PATH ('') 
       ) filters 
    FROM temp a 
    ) aa 

GO 

语法创建使用CTE

CREATE VIEW View_Name AS 
WITH CTE_Name (Columns) AS (SELECT QUERY) 
SELECT QUERY using the CTE Table 
GO 
0
CREATE or replace VIEW final as 
    select aa.email, aa.logintime, aa.region, aa.panelname, aa.numberofdownloads as downloads, case when len(aa.filters) > 0 then left(aa.filters, len(aa.filters)-1) else '' end as filters 
    from (
    Select distinct a.email, a.logintime, a.region, a.panelname, a.numberofdownloads, 
       (
        Select b.filter + ', ' AS [text()] 
        From temp b 
        Where b.panelid=a.panelid 
        ORDER BY b.panelid 
        For XML PATH ('') 
       ) filters 
    from temp a) 
+0

这是行不通的 – mondayguy

+0

这里是临时的声明? – mondayguy

+0

用于查看的一般语法....... 创建视图view_name as //您的选择语句 –

1

视图表的使用条款是选择可选的前缀:

WITH query_name (column_name1, ...) AS 
    (SELECT ...) 

SELECT ... 

也是如此,当with中使用观点:

CREATE VIEW ... 
WITH ... 
SELECT ... 
; 

参见:http://modern-sql.com/feature/with