SQL分页和重复行的计数

时间:2014-07-18 11:36:12

标签: sql sql-server pagination

当我选择查询时,输出将如下所示

EmpID EmpCode   Empname  Date       ID  ClrID   ClrName   Status
100    EMP01    Raj     31 Jul 2014 2   60      clearance   0
100    EMP01    Raj     31 Jul 2014 3   61      Test2       0
101    EMP02    Jino    10 Aug 2014 4   63      Test3       0
102    EMP03    Reegan  30 Jul 2014 5   68      Test4       1
102    EMP03    Reegan  30 Jul 2014 6   113     Test5       1
102    EMP03    Reegan  30 Jul 2014 7   114     Test6       0
103    EMP04    Rajesh  27 May 2014 8   115     Test7       0

但我希望输出像下面和分页

TotalCount  RowNum  RowNumber1 EmpID EmpCode Empname  Date       ID ClrID ClrName   Status
4            1          1       100   EMP01   Raj    31 Jul 2014 2  60    clearance 0
4            1          2       100   EMP01   Raj    31 Jul 2014 3  61    Test2     0
4            2          3       101   EMP02   Jino   10 Aug 2014 4  63    Test3     0
4            3          4       102   EMP03   Reegan 30 Jul 2014 5  68    Test4     1
4            3          5       102   EMP03   Reegan 30 Jul 2014 6  113   Test5     1
4            3          6       102   EMP03   Reegan 30 Jul 2014 7  114   Test6     0
4            4          7       103   EMP04   Rajesh 27 May 2014 8  115   Test7     0

5 个答案:

答案 0 :(得分:2)

您可以使用Windows /分析函数在某些数据库中执行此操作:

select count(distinct empid) over () as TotalCount,
       dense_rank() over (order by empid) as RowNumber,
       row_number() over (order by empid) as RowNumber1,
       t.*
from table t;

请注意,即使某些支持窗口功能的数据库也不支持count(distinct)。但是,有些人这样做。您使用的数据库对此类查询有很大影响。

注意:窗口函数是ANSI标准功能,但并非所有数据库都支持它们。

编辑:

这是在SQL Server上执行count(distinct)的技巧:

select sum(case when empidrownum = 1 then 1 else 0 end) over () as TotalCount,
       dense_rank() over (order by empid) as RowNumber,
       row_number() over (order by empid) as RowNumber1,
       t.*
from (select t.*,
             row_number() over (partition by empid order by empid) as empidrownum
      from table t
     ) t

答案 1 :(得分:0)

这应该做,它将GROUP BY EmpCode然后将加入主记录表,列顺序也应如你的例子中所示。

select x.TotalCount, x.RowNum, ROW_NUMBER() OVER 
 (ORDER BY t.ID) AS RowNumber1, t.* from [table] t
inner join 
(select sum(*) as TotalCount, ROW_NUMBER() OVER 
 (ORDER BY EmpCode) AS RowNum, EmpCode from [table] 
group by EmpCode) as x on x.EmpCode = t.EmpCode

答案 2 :(得分:0)

试试这个:

select 
(select count(distinct empid) from yourtable) TotalCount,
dense_rank() over (order by empid) RowNum,
rank() over (order by id) RowNumber1,
*
from yourtable

这将适用于SQL Server 2005以及Oracle(不确定从哪个版本开始)。

请注意,由于SQL Server在使用distinct over而不是窗口函数时出错,因此我使用子查询来获取TotalCount

答案 3 :(得分:0)

仅限MySQL的解决方案

select count(distinct empid) as totalcount, 
       @empnum := case when @empnum <> empid then @empnum + 1 else @empnum end as rownum,
       @row := @row + 1 as rownumber1,
       *
from your_table
cross join (select @empnum := 0) e
cross join (select @row := 0) r
order by id

答案 4 :(得分:0)

您可以使用dense_rank()和row_number()方法来获取此输出。 我认为你需要这个输出来创建动态表。