网站在读取数据列表页的时候,一个页面可能全部显示一个公司发布的 产品信息,这样对客户友好度不好。 阿里巴巴这方面做的比较不错,他的产品列表页 每一个会员发布的信息在前面值展示一条,其他的都在后边。如有三个公司 A公司 B公司 C公司,每一个公司发布 3条,列表页 没有展示3条
一般的排序可能会出来这个效果 和 阿里的效果页面 普通 阿里
1 A公司 A公司
A公司 B公司
A公司 C公司
---------------------------------
2 B公司 A公司
B公司 B公司
B公司 C公司
---------------------------------
3 C公司 A公司
C公司 B公司
C公司 C公司
---------------------------------阿里的算法是什么 ,要充分考虑 数据读取的性能?
一般的排序可能会出来这个效果 和 阿里的效果页面 普通 阿里
1 A公司 A公司
A公司 B公司
A公司 C公司
---------------------------------
2 B公司 A公司
B公司 B公司
B公司 C公司
---------------------------------
3 C公司 A公司
C公司 B公司
C公司 C公司
---------------------------------阿里的算法是什么 ,要充分考虑 数据读取的性能?
2000加个排序列,
Drop table [tb]
Go
Create table [tb]([ID] int,[P_TITLE] nvarchar(6),[P_COMPANY] nvarchar(3),[P_TIME] Datetime)
Insert tb
Select 1,'aaaaaa','公司1','2009-08-05' union all
Select 2,'bbbbbb','公司1','2009-08-05' union all
Select 3,'vvvvvv','公司1','2009-08-05' union all
Select 4,'cccccc','公司2','2009-08-05' union all
Select 5,'dddddd','公司2','2009-08-05' union all
Select 6,'eeeeee','公司2','2009-08-05' union all
Select 7,'ffffff','公司3','2009-08-05' union all
Select 8,'gggggg','公司3','2009-08-05' union all
Select 9,'hhhhhh','公司3','2009-08-05'
Go
--Select * from tb-->SQL查询如下:
--SQL2000
select * from tb t order by (select count(1) from tb where [P_COMPANY]=t.[P_COMPANY] and id<=t.id),P_TIME desc,[P_COMPANY]
--SQL2005
select * from tb order by row_number()over(partition by [P_COMPANY] order by P_TIME desc),[P_COMPANY]
/*
ID P_TITLE P_COMPANY P_TIME
----------- ------- --------- -----------------------
1 aaaaaa 公司1 2009-08-05 00:00:00.000
4 cccccc 公司2 2009-08-05 00:00:00.000
7 ffffff 公司3 2009-08-05 00:00:00.000
2 bbbbbb 公司1 2009-08-05 00:00:00.000
5 dddddd 公司2 2009-08-05 00:00:00.000
8 gggggg 公司3 2009-08-05 00:00:00.000
3 vvvvvv 公司1 2009-08-05 00:00:00.000
6 eeeeee 公司2 2009-08-05 00:00:00.000
9 hhhhhh 公司3 2009-08-05 00:00:00.000(9 行受影响)*/