执行一个查询而不执行另一个查询时,关键字 'OVER' 附近的语法不正确

Incorrect syntax near the keyword 'OVER' when executing one query but not the other

我正在 iBatis 中处理一个处理分页的查询。

当我运行下面的查询时,它似乎执行得很好:

SELECT
    COUNT(1) OVER (PARTITION BY NULL) AS TotalRows,
    ROW_NUMBER() OVER (ORDER BY [User].Username ASC) AS RowNum
 FROM
    dbo.[USER]
 LEFT OUTER JOIN Account 
                 on [User].AccountId = Account.AccountID
 LEFT OUTER JOIN UserPasscodeAccount on [User].Id = UserPasscodeAccount.UserID
 LEFT OUTER JOIN PasscodeAccount 
                 on UserPasscodeAccount.PasscodeAccountID = PasscodeAccount.ID
 WHERE
    [User].Active = 1

但是,当我尝试将不同的数据子集替换为 "TotalRows" 的值时,出现以下 T-SQL 错误:

Incorrect syntax near the keyword 'OVER'

请查看以下给我错误的查询:

SELECT 
    COUNT(1) OVER (PARTITION BY NULL) FROM (
    SELECT DISTINCT
    [User].Id as "User_Id",
    [User].AccountId as "User_AccountId",
    [User].AccountId as "User_AccountAssociation",
    [User].Username as "User_Username",
    [User].Password as "User_Password",
    [User].Name as "User_Name",
    [User].PhoneNumber as "User_PhoneNumber",
    [User].EmailAddress as "User_EmailAddress",
    [User].CreatedOn as "User_CreatedOn",
    [User].CreatedBy as "User_CreatedBy",
    [User].ChangedOn as "User_ChangedOn",
    [User].ChangedBy as "User_ChangedBy",
    [User].LastLogin as "User_LastLogin",
    [User].TemporaryPassword as "User_TemporaryPassword",
    [User].Active as "User_Active",
    Account.AccountID as "Account_AccountId",
    Account.CompanyID as "Account_CompanyId",
    Account.AccountName as "Account_AccountName",
    Account.AccountType as "Account_AccountType",
    Account.LoginID as "Account_LoginID",
    Account.LoginPassword as "Account_LoginPassword",
    Account.AccountBillingCode as "Account_AccountBillingCode",
    Account.DefaultTimeZone as "Account_DefaultTimeZone",
    Account.AuthorizationLevelID as "Account_AuthorizationLevelID",
    Account.CreatedBy as "Account_CreatedBy",
    Account.ChangedBy as "Account_ChangedBy",
    Account.RegistrationFormID as "Account_RegistrationFormID",
    Account.CreatedOn as "Account_CreatedOn",
    Account.ChangedOn as "Account_ChangedOn",
    Account.DataPresenterID as "Account_DataPresenterID",
    Account.DefaultLoginPIN as "Account_DefaultLoginPIN",
    Account.CopyrightText as "Account_CopyrightText",
    Account.LiveParticipantListEnabled as "Account_LiveParticipantListEnabled",
    Account.MaxLiveParticipantListusers as "Account_MaxLiveParticipantListusers",
    Account.LoginText as "Account_LoginText",
    Account.CMeetingIntegration AS "Account_CMeetingIntegration"
FROM
    [User]
    LEFT OUTER JOIN Account on [User].AccountId = Account.AccountID
    LEFT OUTER JOIN UserPasscodeAccount on [User].Id = UserPasscodeAccount.UserID
    LEFT OUTER JOIN PasscodeAccount on UserPasscodeAccount.PasscodeAccountID = PasscodeAccount.ID
WHERE
    [User].Active = 1
GROUP BY [User].Id,[User].AccountId,[User].AccountId,[User].Username,[User].Password,[User].Name,[User].PhoneNumber,[User].EmailAddress,
         [User].CreatedOn,[User].CreatedBy,[User].ChangedOn,[User].ChangedBy,[User].LastLogin,[User].TemporaryPassword,[User].Active,
         Account.AccountID,Account.CompanyID,Account.AccountName,Account.AccountType,Account.LoginID,Account.LoginPassword,Account.AccountBillingCode,
         Account.DefaultTimeZone,Account.AuthorizationLevelID,Account.CreatedBy,Account.ChangedBy,Account.RegistrationFormID,Account.CreatedOn,
         Account.ChangedOn,Account.DataPresenterID,Account.DefaultLoginPIN,Account.CopyrightText,Account.LiveParticipantListEnabled,
         Account.MaxLiveParticipantListusers,Account.LoginText,Account.CMeetingIntegration)      
 AS TotalRows,
 ROW_NUMBER() OVER (ORDER BY [User].Username ASC) AS RowNum
 FROM
    dbo.[USER]
 LEFT OUTER JOIN Account on [User].AccountId = Account.AccountID
 LEFT OUTER JOIN UserPasscodeAccount on [User].Id = UserPasscodeAccount.UserID
 LEFT OUTER JOIN PasscodeAccount on UserPasscodeAccount.PasscodeAccountID = PasscodeAccount.ID
 WHERE
    [User].Active = 1

请注意,上面的查询将正常执行,直到它命中以下行:

ROW_NUMBER() OVER (ORDER BY [User].Username ASC) AS RowNum

我想我的问题是 "Why does the first query run fine, but when I attempt to use a subset of data to populate 'TotalRows' I get an incorrect syntax error regarding the OVER clause for 'RowNum' as in the statement above?"

我不是很懂T-SQL.

,请多多包涵

如果您有任何疑问或需要进一步说明,我很乐意提供帮助。

您的语法在 ROW_NUMBER() OVER ( ORDER BY [User].Username ASC)) AS RowNum

附近有误

您可以修改查询如下

        ;with cte as (
            SELECT DISTINCT
            [User].Id as "User_Id",
            [User].AccountId as "User_AccountId",
            [User].AccountId as "User_AccountAssociation",
            [User].Username as "User_Username",
            [User].Password as "User_Password",
            [User].Name as "User_Name",
            [User].PhoneNumber as "User_PhoneNumber",
            [User].EmailAddress as "User_EmailAddress",
            [User].CreatedOn as "User_CreatedOn",
            [User].CreatedBy as "User_CreatedBy",
            [User].ChangedOn as "User_ChangedOn",
            [User].ChangedBy as "User_ChangedBy",
            [User].LastLogin as "User_LastLogin",
            [User].TemporaryPassword as "User_TemporaryPassword",
            [User].Active as "User_Active",
            Account.AccountID as "Account_AccountId",
            Account.CompanyID as "Account_CompanyId",
            Account.AccountName as "Account_AccountName",
            Account.AccountType as "Account_AccountType",
            Account.LoginID as "Account_LoginID",
            Account.LoginPassword as "Account_LoginPassword",
            Account.AccountBillingCode as "Account_AccountBillingCode",
            Account.DefaultTimeZone as "Account_DefaultTimeZone",
            Account.AuthorizationLevelID as "Account_AuthorizationLevelID",
            Account.CreatedBy as "Account_CreatedBy",
            Account.ChangedBy as "Account_ChangedBy",
            Account.RegistrationFormID as "Account_RegistrationFormID",
            Account.CreatedOn as "Account_CreatedOn",
            Account.ChangedOn as "Account_ChangedOn",
            Account.DataPresenterID as "Account_DataPresenterID",
            Account.DefaultLoginPIN as "Account_DefaultLoginPIN",
            Account.CopyrightText as "Account_CopyrightText",
            Account.LiveParticipantListEnabled as "Account_LiveParticipantListEnabled",
            Account.MaxLiveParticipantListusers as "Account_MaxLiveParticipantListusers",
            Account.LoginText as "Account_LoginText",
            Account.CMeetingIntegration AS "Account_CMeetingIntegration"
        FROM
            [User]
            LEFT OUTER JOIN Account on [User].AccountId = Account.AccountID
            LEFT OUTER JOIN UserPasscodeAccount on [User].Id = UserPasscodeAccount.UserID
            LEFT OUTER JOIN PasscodeAccount on UserPasscodeAccount.PasscodeAccountID = PasscodeAccount.ID
        WHERE
            [User].Active = 1
        GROUP BY [User].Id,[User].AccountId,[User].AccountId,[User].Username,[User].Password,[User].Name,[User].PhoneNumber,[User].EmailAddress,
                 [User].CreatedOn,[User].CreatedBy,[User].ChangedOn,[User].ChangedBy,[User].LastLogin,[User].TemporaryPassword,[User].Active,
                 Account.AccountID,Account.CompanyID,Account.AccountName,Account.AccountType,Account.LoginID,Account.LoginPassword,Account.AccountBillingCode,
                 Account.DefaultTimeZone,Account.AuthorizationLevelID,Account.CreatedBy,Account.ChangedBy,Account.RegistrationFormID,Account.CreatedOn,
                 Account.ChangedOn,Account.DataPresenterID,Account.DefaultLoginPIN,Account.CopyrightText,Account.LiveParticipantListEnabled,
                 Account.MaxLiveParticipantListusers,Account.LoginText,Account.CMeetingIntegration)
        SELECT 
            COUNT(1) OVER (PARTITION BY NULL) AS TotalRows,
         ROW_NUMBER() OVER ( ORDER BY cte.Username ASC) AS RowNum
         FROM  cte