日期:2014-05-19  浏览次数:20499 次

急~求一条sql语句
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 张三


------解决方案--------------------
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