SQL查询返回正确的行号

时间:2015-09-26 12:45:11

标签: sql-server sql-server-2008

以下查询返回RowNumber_ps = 101RowNumber_ps是行计数,但是它应该返回RowNumber_ps = 1,因为我只有一行包含0个文档。任何人都可以告诉我RowNumber_ps没有返回1的查询有什么问题吗?

SELECT *
FROM 
    (SELECT 
         ROW_NUMBER() OVER(Order By ParentID) AS RowNumber_ps,
         UPPER(HostApplicationLocalData.ParentID) asParentID,
         COUNT(Document.ID) AS Documents 
     FROM 
         HostApplicationLocalData 
     LEFT OUTER JOIN 
         Document ON HostApplicationLocalData.ID = Document.HostApplicationLocalData_ID
     WHERE 
         HostApplicationLocalData.TransactionType_ID = 1
         AND HostApplicationLocalData.Company_ID = 9000
     GROUP BY 
         HostApplicationLocalData.ParentID) q
WHERE
     Documents = '0'

1 个答案:

答案 0 :(得分:-1)

您应该在子查询中放置'WHERE'条件。