带有Group By的SQL Server Row_Number()

时间:2015-08-21 15:37:36

标签: sql-server sql-server-2008

SQL Server 2008中的视图具有Group By,它将记录精炼为23. ROW_Number sproc中的相同查询返回58条记录。我已尝试各种组合分区以返回相同的结果,尝试过所有字段(如View)或其他配对但没有任何作用。

希望有人可以对此有所了解。

未正确分组的存储过程(返回58条记录)。 @Filter包含johndoe,就像View(见下文):

{ "SomeData": "Sample={\"Id\":-1,\"Key\":\"test\"}" }

具有分组依据的视图返回23条记录:

ALTER PROCEDURE [dbo].[Get_Reporting2]
@OutTotalRecCount INT OUTPUT, 
@CurrentPage INT, 
@PageSize INT, 
@SortDirection INT,
@SortField nvarchar(50),
@Filter nvarchar(50) 
AS     
SELECT *
FROM          
(SELECT ROW_NUMBER() OVER (
    PARTITION BY 
    --dbo.ppoma_fsa.OBJID, 
    --dbo.ppoma_fsa.OTYPE, 
    --dbo.ppoma_fsa.STEXT, 
    --dbo.ppoma_fsa.LOW,
    --dbo.ppoma_assigned.SOBID, 
    dbo.ppoma_us_bp.Personal_No, 
    dbo.ppoma_us_bp.UserID, 
    dbo.ppoma_us_bp.BP, 
    dbo.ppoma_us_bp.NAME_FIRST, 
    dbo.ppoma_us_bp.NAME_LAST

    --dbo.ppoma_us_bp.UserID, 
    --dbo.ppoma_fsa.LOW

    ORDER BY
    CASE WHEN @SortDirection = 1 AND @SortField = 1 THEN ppoma_fsa.OBJID END DESC,  
    CASE WHEN @SortDirection = 1 AND @SortField = 2 THEN ppoma_fsa.OTYPE END ASC, 
    CASE WHEN @SortDirection = 2 AND @SortField = 2 THEN ppoma_fsa.OTYPE END DESC   
    ) 
    AS Row,

dbo.ppoma_fsa.OBJID as _OBJID, 
dbo.ppoma_fsa.OTYPE as _OTYPE, 
dbo.ppoma_fsa.STEXT as _STEXT, 
dbo.ppoma_fsa.LOW,
dbo.ppoma_assigned.SOBID AS Depot_BP, 
dbo.ppoma_us_bp.Personal_No, 
dbo.ppoma_us_bp.UserID, 
dbo.ppoma_us_bp.BP, 
dbo.ppoma_us_bp.NAME_FIRST, 
dbo.ppoma_us_bp.NAME_LAST

FROM         dbo.ppoma_assigned INNER JOIN
                      dbo.ppoma_us_bp ON dbo.ppoma_assigned.SOBID = dbo.ppoma_us_bp.OBJID INNER JOIN
                      dbo.ppoma_fsa ON dbo.ppoma_assigned.OBJID = dbo.ppoma_fsa.OBJID
WHERE     (dbo.ppoma_us_bp.UserID LIKE N'%' + @Filter + '%') AND (NOT (dbo.ppoma_fsa.LOW = 'Service'))

)

AS TeamWithRowNumbers 
WHERE  Row >= (@CurrentPage - 1) * @PageSize + 1 AND Row <= @CurrentPage*@PageSize     


SELECT @OutTotalRecCount = COUNT(*)

FROM         dbo.ppoma_assigned INNER JOIN
                      dbo.ppoma_us_bp ON dbo.ppoma_assigned.SOBID = dbo.ppoma_us_bp.OBJID INNER JOIN
                      dbo.ppoma_fsa ON dbo.ppoma_assigned.OBJID = dbo.ppoma_fsa.OBJID
WHERE     (dbo.ppoma_us_bp.UserID LIKE N'%' + @Filter + '%') AND (NOT (dbo.ppoma_fsa.LOW = 'Service'))

2 个答案:

答案 0 :(得分:0)

如果没有看到任何样本数据,就无法确定。

可能是因为proc在filter参数上使用LIKE,视图使用=

存储过程:

WHERE     (dbo.ppoma_us_bp.UserID LIKE N'%' + @Filter + '%')

查看:

HAVING      (dbo.ppoma_us_bp.UserID = 'johndoe')

此外,假设视图使用GROUP BY,如果数据中有任何重复项,则很可能返回较少的行。 ROW_NUMBER函数绝不像GROUP BY,它们根本不做同样的事情。

GROUP BY用于聚合,通常是为了执行SUM或其他聚合函数:https://msdn.microsoft.com/en-us/library/ms177673.aspx

ROW_NUMBER仅用于为结果集中的行分配序号:https://msdn.microsoft.com/en-us/library/ms186734.aspx

我不明白的是,如果你想要与视图相同的结果,为什么不选择它?

答案 1 :(得分:0)

我的解决方案是将View转换为Make Table(ppoma_final),然后将Row_Number()修改为以下内容。像魅力一样。

SELECT *
FROM          
(SELECT ROW_NUMBER() OVER (

    ORDER BY
    CASE WHEN @SortDirection = 1 AND @SortField = 1 THEN ppoma_final.OBJID END DESC,    
    CASE WHEN @SortDirection = 1 AND @SortField = 2 THEN ppoma_final.OTYPE END ASC, 
    CASE WHEN @SortDirection = 2 AND @SortField = 2 THEN ppoma_final.OTYPE END DESC 
    ) 
    AS Row,

    [OBJID]
    ,[OTYPE]
    ,[STEXT]
    ,[LOW]
    ,[Depot_BP]
    ,[Personal_No]
    ,[UserID]
    ,[BP]
    ,[NAME_FIRST]
    ,[NAME_LAST]
    ,[id_fsa]
    ,[id_us_bp]



FROM         dbo.ppoma_final
WHERE     (dbo.ppoma_final.UserID LIKE N'%' + @Filter + '%') AND (NOT (dbo.ppoma_final.LOW = 'Service'))

)

AS TeamWithRowNumbers 
WHERE  Row >= (@CurrentPage - 1) * @PageSize + 1 AND Row <= @CurrentPage*@PageSize     


SELECT @OutTotalRecCount = COUNT(*)

FROM         dbo.ppoma_final
WHERE     (dbo.ppoma_final.UserID LIKE N'%' + @Filter + '%') AND (NOT (dbo.ppoma_final.LOW = 'Service'))