关于统计数据
要统计推荐数最多的排行,表memberlist里面的mRcommendId是推荐人
需要统计推荐人的taoc字段(此字段为int)排行列出来然后需要将其他信息,比如mphone,membername都列出来
我写了下
select top 200 mRcommendId,sum(taoc) as 次数
from memberlist
group by mRcommendId
having sum(taoc)>1
order by 2 desc
这个能排列出前200名的会员,但是我需要把mphone 和 membername 一起显示出来。需要怎么写?
还有需要统计另外一个表
SELECT top 200 mmembercardid,cnt=sum(msummoney)
FROM MemberPrixList
GROUP BY mmembercardid
ORDER BY cnt DESC
已经写好了,可以统计出 msummoney的这个累加最多的200名排行。 但是需要和memberlist一起查,MemberPrixList表的mmembercardid和memberlist表里面的mmembercardid对应的。也是需要查到mphone和membername一起显示出来。
------解决方案-------------------- 引用: Quote: 引用:
那就这样:
select top 200
t1.mRcommendId,
sum(t1.taoc) as 次数,
MAX(t2.mphone) mphone,
max(t2.membername) membername
from memberlist t1
inner join memberlist t2
on t1.mRcommendId = t2.mmembercardid
group by t1.mRcommendId
having sum(t1.taoc)>1
order by 2 desc
可以了。感谢。顺便问价,如果有条件怎么加啊?在from memberlist t1后面加where ti.msd<>1 提示出错。
试试这个:
select top 200
t1.mRcommendId,
sum(t1.taoc) as 次数,
MAX(t2.mphone) mphone,
max(t2.membername) membername
from memberlist t1
inner join memberlist t2
on t1.mRcommendId = t2.mmembercardid
where ti.msd<>1 --加在这里试试
group by t1.mRcommendId
having sum(t1.taoc)>1
order by 2 desc