CREATE TABLE [dbo].[mytest](
[keyword] [varchar](200) ,
[Sort] varchar(10) NULL,
[totalViews] [int] NULL,
userName varchar(20) NULL

[keyword] 代表歌曲
Sort 代表歌曲类别
totalViews 代表点击率
userName 代表用户
insert mytest 
select '我们的爱','流行',4,'张三' union all
select '高山流水','古典音乐',6,'张三' union all
select '天命最高','流行',7,'张三' union all
select '光辉岁月','流行',9,'张三' union all
select '千年之恋','古典音乐',10,'张三' union all
select '大城小爱','古典音乐',11,'张三' union all
select '受了点伤','流行',23,'张三' union all
select '不再犹豫','流行',14,'张三' union all
select '我们的爱','流行',40,'李四' union all
select '高山流水','古典音乐',16,'李四' union all
select '天命最高','流行',67,'李四' union all
select '光辉岁月','流行',29,'李四' union all
select '千年之恋','古典音乐',15,'李四' union all
select '大城小爱','古典音乐',10,'李四' union all
select '受了点伤','流行',22,'李四' union all
select '不再犹豫','流行',12,'李四' union all
select '我们的爱','流行',4,'王五' union all
select '高山流水','古典音乐',36,'王五' union all
select '天命最高','古典音乐',67,'王五' union all
select '光辉岁月','古典音乐',29,'王五' union all
select '千年之恋','流行',35,'王五' union all
select '大城小爱','流行',60,'王五' union all
select '受了点伤','流行',82,'王五' union all
select '不再犹豫','流行',12,'王五'  -- ..........还有很多的用户--我的要求是: 查出每位用户在不同分类下 最喜爱的三首歌曲,根据点击率!!
-- 如 
 -- 张三:数据显示如下:
    大城小爱 流行 11 张三
受了点伤 流行 23 张三
不再犹豫 流行 14 张三
  千年之恋 古典音乐 10 张三
  大城小爱 古典音乐 11 张三  
  高山流水 古典音乐 6 张三

解决方案 »

  1.   

    Select * From mytest A
    Where (Select Count(*) From mytest Where userName = A.userName And Sort = A.Sort And totalViews > A.totalViews) < 3
    Order By userName, Sort, totalViews Desc
      

  2.   

    Where (Select Count(*) From mytest Where userName = A.userName And Sort = A.Sort And totalViews > A.totalViews) < 3 
    这有点不明白~能解释下吗
      

  3.   

    CREATE TABLE [#mytest](
    [keyword] [varchar](200) ,
    [Sort] varchar(10) NULL,
    [totalViews] [int] NULL,
    userName varchar(20) NULL

    --[keyword] 代表歌曲
    --Sort 代表歌曲类别
    --totalViews 代表点击率
    --userName 代表用户
    insert #mytest 
    select '我们的爱','流行',4,'张三' union all
    select '高山流水','古典音乐',6,'张三' union all
    select '天命最高','流行',7,'张三' union all
    select '光辉岁月','流行',9,'张三' union all
    select '千年之恋','古典音乐',10,'张三' union all
    select '大城小爱','古典音乐',11,'张三' union all
    select '受了点伤','流行',23,'张三' union all
    select '不再犹豫','流行',14,'张三' union all
    select '我们的爱','流行',40,'李四' union all
    select '高山流水','古典音乐',16,'李四' union all
    select '天命最高','流行',67,'李四' union all
    select '光辉岁月','流行',29,'李四' union all
    select '千年之恋','古典音乐',15,'李四' union all
    select '大城小爱','古典音乐',10,'李四' union all
    select '受了点伤','流行',22,'李四' union all
    select '不再犹豫','流行',12,'李四' union all
    select '我们的爱','流行',4,'王五' union all
    select '高山流水','古典音乐',36,'王五' union all
    select '天命最高','古典音乐',67,'王五' union all
    select '光辉岁月','古典音乐',29,'王五' union all
    select '千年之恋','流行',35,'王五' union all
    select '大城小爱','流行',60,'王五' union all
    select '受了点伤','流行',82,'王五' union all
    select '不再犹豫','流行',12,'王五' select *
    from #mytest--生成一个临时表
    select max(keyword) as keyword,sort,sum(totalviews) as totalviews,username into #tmp from #mytest a 
    group by username,sort,keyword
    order by username,sort,totalviews descselect * from #tmp--通过临时表得到你要的结果
    select * from #tmp as #a
    where totalviews in (select top 3 totalviews from #tmp where username=#a.username and sort=#a.sort)
      

  4.   

    Select * From mytest A
    Where (Select Count(*) From mytest Where userName = A.userName And Sort = A.Sort And totalViews > A.totalViews) < 3
    Order By userName, Sort, totalViews Desc
    ----------------------
    以上代码非常好,学习了.
      

  5.   

    --生成一个临时表
    select max(keyword) as keyword,sort,sum(totalviews) as totalviews,username into #tmp from #mytest a 
    group by username,sort,keyword
    order by username,sort,totalviews descselect * from #tmp--通过临时表得到你要的结果
    select * from #tmp as #a
    where totalviews in (select top 3 totalviews from #tmp where username=#a.username and sort=#a.sort)
    ???SQL有逻辑错误吧sum(totalviews)???这是什么意思
      

  6.   

    ???SQL有逻辑错误吧sum(totalviews)???这是什么意思
    ----------------
    没错呀,totalviews是被点的次数,对它求和以查看被点的总数呀,难道有错吗?