首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

急求一条sql语句解决方法

2011-12-27 
急~求一条sql语句CREATETABLE[dbo].[mytest]([keyword][varchar](200),[Sort]varchar(10)NULL,[totalViews

急~求一条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

热点排行
Bad Request.