网站的数据 比较大 1000万的样子是产品数据库  放了 不同公司的产品  每个公司 可以会出现产品相同的情况要求 一页显示4条记录  符合条件的数据都要显示 每一页只显示一个公司 ,到后面没有符合4个公司的数据  可以出现重复公司  排序方法 是 按  时间 desc 产品 asc 
搜索 汽车 这个词语create table  #temp
(
   company varchar(50),
   product varchar(50),
   inputDate datetime
)insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车1','2010-8-1')
insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车2','2010-8-1')
insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车3','2010-8-1')
insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车4','2010-8-1')
insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车5','2010-7-1')insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车1','2010-8-1')
insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车2','2010-8-1')
insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车3','2010-8-1')
insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车4','2010-8-1')insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车1','2010-8-1')
insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车2','2010-8-1')
insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车3','2010-8-1')
insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车4','2010-8-1')insert into #temp(company,product,inputDate) values('天津旺旺有限公司','汽车4','2010-8-1')
insert into #temp(company,product,inputDate) values('天津旺旺有限公司','汽车5','2010-8-1')select * from #tempdrop table #temp 
第一页
杭州大明有限公司  汽车1 2010-8-1
北京小科有限公司  汽车1 2010-8-1
上海有得有限公司  汽车1 2010-8-1
天津旺旺有限公司  汽车4 2010-8-1第二页
杭州大明有限公司  汽车2 2010-8-1
北京小科有限公司  汽车2 2010-8-1
上海有得有限公司  汽车2 2010-8-1
天津旺旺有限公司  汽车5 2010-8-1第三页
杭州大明有限公司  汽车3 2010-8-1
北京小科有限公司  汽车3 2010-8-1
上海有得有限公司  汽车3 2010-8-1
杭州大明有限公司  汽车4 2010-8-1   
第四页北京小科有限公司  汽车4 2010-8-1
上海有得有限公司  汽车4 2010-8-1
杭州大明有限公司  汽车5 2010-7-1  
需要用分页存储过程,因为数据量 比较大  

解决方案 »

  1.   

    用分布存储过程,并且可以加条件
    /*
    drop proc proc_pageviewdeclare @RecordCount int
    exec proc_pageview 
    @tbname='tbStockCheck_view',
    @FieldKey='id',
    @PageCurrent=2,
    @PageSize=10,
    @FieldShow='',
    @FieldOrder='create_date desc',
    @Where=' 1=1 ',
    @RecordCount=1*/
    CREATE PROC proc_pageview
    (
    @tbname     sysname,               --要分页显示的表名
    @FieldKey   nvarchar(1000),      --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
    @PageCurrent int=1,               --要显示的页码
    @PageSize   int=10,                --每页的大小(记录数)
    @FieldShow nvarchar(1000)='',      --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段
    @FieldOrder nvarchar(1000)='',      --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC
    @Where    varchar(1000)='',     --查询条件
    @RecordCount int OUTPUT             --总页数
    )
    AS
    SET NOCOUNT ON
    --检查对象是否有效
    IF OBJECT_ID(@tbname) IS NULL
    BEGIN
        RAISERROR(N'对象"%s"不存在',1,16,@tbname)
        RETURN
    END
    IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
        AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
        AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
    BEGIN
        RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@tbname)
        RETURN
    END--分页字段检查
    IF ISNULL(@FieldKey,N'')=''
    BEGIN
        RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)
        RETURN
    END--其他参数检查及规范
    IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
    IF ISNULL(@PageSize,0)<1 SET @PageSize=10
    IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
    IF ISNULL(@FieldOrder,N'')=N''
        SET @FieldOrder=N''
    ELSE
        SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
    IF ISNULL(@Where,N'')=N''
        SET @Where=N''
    ELSE
        SET @Where=N'WHERE ('+@Where+N')'--如果@PageCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@PageCount赋值)
    IF @RecordCount IS NULL
    BEGIN
        DECLARE @sql nvarchar(4000)
        SET @sql=N'SELECT @RecordCount=COUNT(*)'
            +N' FROM '+@tbname
            +N' '+@Where
        EXEC sp_executesql @sql,N'@RecordCount int OUTPUT',@RecordCount OUTPUT
    END--计算分页显示的TOPN值
    DECLARE @TopN varchar(20),@TopN1 varchar(20)
    SELECT @TopN=@PageSize,
        @TopN1=(@PageCurrent-1)*@PageSize--第一页直接显示
    IF @PageCurrent=1
        EXEC(N'SELECT TOP '+@TopN
            +N' '+@FieldShow
            +N' FROM '+@tbname
            +N' '+@Where
            +N' '+@FieldOrder)
    ELSE
    BEGIN
        --处理别名
        IF @FieldShow=N'*'
            SET @FieldShow=N'a.*'
        --生成主键(惟一键)处理条件
        DECLARE @Where1 nvarchar(4000),@Where2 nvarchar(4000),
            @s nvarchar(1000),@Field sysname
        SELECT @Where1=N'',@Where2=N'',@s=@FieldKey
        WHILE CHARINDEX(N',',@s)>0
            SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),
                @s=STUFF(@s,1,CHARINDEX(N',',@s),N''),
                @Where1=@Where1+N' AND a.'+@Field+N'=b.'+@Field,
                @Where2=@Where2+N' AND b.'+@Field+N' IS NULL',
                @Where=REPLACE(@Where,@Field,N'a.'+@Field),
                @FieldOrder=REPLACE(@FieldOrder,@Field,N'a.'+@Field),
                @FieldShow=REPLACE(@FieldShow,@Field,N'a.'+@Field)
        SELECT --@Where=REPLACE(@Where,@s,N'a.'+@s),
            @FieldOrder=REPLACE(@FieldOrder,@s,N'a.'+@s),
            @FieldShow=REPLACE(@FieldShow,@s,N'a.'+@s),
            @Where1=STUFF(@Where1+N' AND a.'+@s+N'=b.'+@s,1,5,N''),    
            @Where2=CASE
                WHEN @Where='' THEN N'WHERE ('
                ELSE @Where+N' AND ('
                END+N'b.'+@s+N' IS NULL'+@Where2+N')'
        --执行查询
        exec(N'SELECT TOP '+@TopN
            +N' '+@FieldShow
            +N' FROM '+@tbname
            +N' a LEFT JOIN(SELECT TOP '+@TopN1
            +N' '+@FieldKey
            +N' FROM '+@tbname
            +N' a '+@Where
            +N' '+@FieldOrder
            +N')b ON '+@Where1
            +N' '+@Where2
            +N' '+@FieldOrder)
    END
    GO
      

  2.   


    create table #temp
    (
      company varchar(50),
      product varchar(50),
      inputDate datetime
    )insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车1','2010-8-1')
    insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车2','2010-8-1')
    insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车3','2010-8-1')
    insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车4','2010-8-1')
    insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车5','2010-7-1')insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车1','2010-8-1')
    insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车2','2010-8-1')
    insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车3','2010-8-1')
    insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车4','2010-8-1')insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车1','2010-8-1')
    insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车2','2010-8-1')
    insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车3','2010-8-1')
    insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车4','2010-8-1')insert into #temp(company,product,inputDate) values('天津旺旺有限公司','汽车4','2010-8-1')
    insert into #temp(company,product,inputDate) values('天津旺旺有限公司','汽车5','2010-8-1')with t1 as
    (select rn1=row_number()over(partition by company order by inputDate),ct=count(1)over(partition by company) ,* from #temp )
    ,t2 as
    (select rn2=row_number()over( order by rn1,ct desc) ,* from t1  )
    ,t3 as
    (select rn3=dense_rank()over( order by (rn2-1)/4) ,* from t2)
    select * from t3 order by rn3
    --最终的rn3是页码,如果不是sql2005就很麻烦了,这样还不知道效率是否过关。。
    /*
    rn3                  rn2                  rn1                  ct          company                                            product                                            inputDate
    -------------------- -------------------- -------------------- ----------- -------------------------------------------------- -------------------------------------------------- -----------------------
    1                    1                    1                    5           杭州大明有限公司                                           汽车5                                                2010-07-01 00:00:00.000
    1                    2                    1                    4           北京小科有限公司                                           汽车1                                                2010-08-01 00:00:00.000
    1                    3                    1                    4           上海有得有限公司                                           汽车1                                                2010-08-01 00:00:00.000
    1                    4                    1                    2           天津旺旺有限公司                                           汽车4                                                2010-08-01 00:00:00.000
    2                    5                    2                    5           杭州大明有限公司                                           汽车1                                                2010-08-01 00:00:00.000
    2                    6                    2                    4           北京小科有限公司                                           汽车2                                                2010-08-01 00:00:00.000
    2                    7                    2                    4           上海有得有限公司                                           汽车2                                                2010-08-01 00:00:00.000
    2                    8                    2                    2           天津旺旺有限公司                                           汽车5                                                2010-08-01 00:00:00.000
    3                    9                    3                    5           杭州大明有限公司                                           汽车2                                                2010-08-01 00:00:00.000
    3                    10                   3                    4           北京小科有限公司                                           汽车3                                                2010-08-01 00:00:00.000
    3                    11                   3                    4           上海有得有限公司                                           汽车3                                                2010-08-01 00:00:00.000
    3                    12                   4                    5           杭州大明有限公司                                           汽车3                                                2010-08-01 00:00:00.000
    4                    13                   4                    4           北京小科有限公司                                           汽车4                                                2010-08-01 00:00:00.000
    4                    14                   4                    4           上海有得有限公司                                           汽车4                                                2010-08-01 00:00:00.000
    4                    15                   5                    5           杭州大明有限公司                                           汽车4                                                2010-08-01 00:00:00.000(15 行受影响)*/
      

  3.   

    sql2000期待解答,想了很久,没什么好的办法。
      

  4.   

    SQL2000
    create table #temp
    (
      company varchar(50),
      product varchar(50),
      inputDate datetime
    )insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车1','2010-8-1')
    insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车2','2010-8-1')
    insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车3','2010-8-1')
    insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车4','2010-8-1')
    insert into #temp(company,product,inputDate) values('杭州大明有限公司','汽车5','2010-7-1')insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车1','2010-8-1')
    insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车2','2010-8-1')
    insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车3','2010-8-1')
    insert into #temp(company,product,inputDate) values('北京小科有限公司','汽车4','2010-8-1')insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车1','2010-8-1')
    insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车2','2010-8-1')
    insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车3','2010-8-1')
    insert into #temp(company,product,inputDate) values('上海有得有限公司','汽车4','2010-8-1')insert into #temp(company,product,inputDate) values('天津旺旺有限公司','汽车4','2010-8-1')
    insert into #temp(company,product,inputDate) values('天津旺旺有限公司','汽车5','2010-8-1')select * from #temp
    create proc getdata@num int 
    asbeginselect  top 4 *  from  (
    select ( select count(*) from #temp where company=a.company and product<=a.product) as 序号,a.company,a.product,a.inputDate
    from #temp a 
    ) b 
    where 序号>=@numorder by  序号,inputDate descendgo
    getdata 4
    /*结果1 杭州大明有限公司 汽车1 2010-08-01 00:00:00.000
    1 北京小科有限公司 汽车1 2010-08-01 00:00:00.000
    1 上海有得有限公司 汽车1 2010-08-01 00:00:00.000
    1 天津旺旺有限公司 汽车4 2010-08-01 00:00:00.000
    2 天津旺旺有限公司 汽车5 2010-08-01 00:00:00.000
    2 上海有得有限公司 汽车2 2010-08-01 00:00:00.000
    2 北京小科有限公司 汽车2 2010-08-01 00:00:00.000
    2 杭州大明有限公司 汽车2 2010-08-01 00:00:00.000
    3 杭州大明有限公司 汽车3 2010-08-01 00:00:00.000
    3 北京小科有限公司 汽车3 2010-08-01 00:00:00.000
    3 上海有得有限公司 汽车3 2010-08-01 00:00:00.000
    4 北京小科有限公司 汽车4 2010-08-01 00:00:00.0004 北京小科有限公司 汽车4 2010-08-01 00:00:00.000
    4 上海有得有限公司 汽车4 2010-08-01 00:00:00.000
    4 杭州大明有限公司 汽车4 2010-08-01 00:00:00.000
    5 杭州大明有限公司 汽车5 2010-07-01 00:00:00.000
    */
      

  5.   

    --sql2005
    create proc getdata2005
    @num int 
    as
    begin
    select  top 4 *  from 
    (
    select row_number() over (partition by company order by product ) as 序号,a.company,a.product,a.inputDate
    from #temp a 
    ) b 
    where 序号>=@num
    order by  序号,inputDate desc
    end