无法在Sql Server中运行分析功能

时间:2012-01-09 16:18:59

标签: sql-server database

在Oracle中运行良好。查询oracle是As Follows

SELECT TEAM_ID, LEVEL_ID, FF_ID, MODULE_ID, TERR_ID, MERGE_KEY, count(distinct TERR_ID) over (partition by TEAM_ID,LEVEL_ID,FF_ID,MODULE_ID)||' of '||count(distinct TERR_ID) over (partition by TEAM_ID,LEVEL_ID,FF_ID) as MISMATCH_TERR_COUNT_IN_FF FROM SCN7BBFE80210E04E2F88653A.PA83FB9BD57E044618B7AC86A;

但对于SQL Server,我收到错误

我创建了一个表,其列名为TEAM_ID,LEVEL_ID,FF_ID,MODULE_ID,TERR_ID,MERGE_KEY,然后将值插入表中...

insert into PA83FB9BD57E044618B7AC86A values('3',1,'YH','PERF','3XBDA3',1)
insert into PA83FB9BD57E044618B7AC86A values('3',1,'YH','PERF','3XAJA3',1) 

这是SQL查询...

SELECT  TEAM_ID ,
        LEVEL_ID ,
        FF_ID ,
        MODULE_ID ,
        TERR_ID ,
        MERGE_KEY ,
        ISNULL(CAST(COUNT(DISTINCT TERR_ID) OVER ( PARTITION BY TEAM_ID,
                                 LEVEL_ID, FF_ID, MODULE_ID ) AS NVARCHAR(MAX)),
               '') + ' of '
        + ISNULL(CAST(COUNT(DISTINCT TERR_ID) OVER ( PARTITION BY TEAM_ID,
                                 LEVEL_ID, FF_ID ) AS NVARCHAR(MAX)),
                 '') AS MISMATCH_TERR_COUNT_IN_FF
FROM    dbo.PA83FB9BD57E044618B7AC86A

运行时的上述查询给出错误:'distinct'附近的语法不正确。

使查询变小我试过

SELECT TEAM_ID, LEVEL_ID, FF_ID, MODULE_ID, TERR_ID, MERGE_KEY, cast(count(distinct TERR_ID) OVER(PARTITION BY TEAM_ID, LEVEL_ID, FF_ID, MODULE_ID) AS MISMATCH_TERR_COUNT_IN_FF FROM dbo.PA83FB9BD57E044618B7AC86A 

错误:'distinct'附近的语法不正确

我尝试删除Cast部分,但同样的错误再次出现

任何人都可以帮助.......

1 个答案:

答案 0 :(得分:1)

您不能在count(distinct col)子句中使用over。你必须做一个子查询,用count(distinct col)做一个老式的group by做你想做的事。