22,209
社区成员
发帖
与我相关
我的任务
分享
select top 4 tid,sum(alink) as salink from anews t
--where timecol between begintime and endtime 加在这个位置
group by tid
order by salink desc
--求和后最好的前4个tid
select top 4 tid,sum(alink) as salink from anews t group by tid
order by salink desc
--单个值最高的前4个tid
select top 4 tid from anews t where alink
=(select max(alink) from anews where tid=t.tid)
order by alink desc
--求和后最好的前4个tid
select top 4 tid,sum(alink) as salink from anews t group by tid
order by salink desc
declare @T table (aid int,aname varchar(2),alink int,tid int)
insert into @T
select 1,'啊',2000,1 union all
select 2,'啊',1999,1 union all
select 3,'啊',50,2 union all
select 4,'啊',1500,3 union all
select 5,'啊',1501,3 union all
select 6,'啊',2000,3 union all
select 7,'啊',1300,4 union all
select 8,'啊',1800,5 union all
select 9,'啊',1700,6
--求和结果也是3 1 5 6
select top 4 tid,sum(alink) as salink from @T t group by tid
order by salink desc
/*
tid salink
----------- -----------
3 5001
1 3999
5 1800
6 1700
*/
select top 4 tid,SUM(alink) as 总销量
from 信息表 a join 类别表 b on a.tid=b.tid
group by tid
order by 2 desc
select tid,SUM(alink) as 总销量
from 信息表 a join 类别表 b on a.tid=b.tid
group by tid
order by 2
declare @T table (aid int,aname varchar(2),alink int,tid int)
insert into @T
select 1,'啊',2000,1 union all
select 2,'啊',1999,1 union all
select 3,'啊',50,2 union all
select 4,'啊',1500,3 union all
select 5,'啊',1501,3 union all
select 6,'啊',2000,3 union all
select 7,'啊',1300,4 union all
select 8,'啊',1800,5 union all
select 9,'啊',1700,6
select top 4 tid from @T t where alink
=(select max(alink) from @T where tid=t.tid) order by alink desc
/*
tid
-----------
3
1
5
6
*/