-1

我有logs一个名为记录谁输入或输出数据的表。现在我不想得到谁贡献最多的统计数据并对其进行排名。

列是

 Occur_Time | iUser_id | iUsername | oUser_id | oUsername
 --iUser_id is the input persons index from another table that lists the username.
 --iUsername is the input persons name.
 --oUser_id is the index of the person who took the input away.
 --oUsername is the name of the person who took the input away.

现在我想知道谁的投入最多。

我的逻辑:

 Example:
     User_id is 1, name is One.
     Check how many times 1 is repeated on iUser_id = 100 times.
     Check how many times 1 is repeated on oUser_id = 10 times.
     User_id=1 has contributed 90 times.
     Then sort by who has most contribution.

谢谢你。

4

3 回答 3

0

Rank功能可能是您正在寻找的。

http://msdn.microsoft.com/en-us/library/ms176102.aspx

于 2012-07-26T15:05:57.147 回答
0

(未经测试):

SELECT L.iUsername, 
((SELECT COUNT(1) FROM logs WHERE iUsername=L.iUsername) - 
(SELECT COUNT(1) FROM logs WHERE oUsername=L.iUsername)) as rank  
FROM logs L 
GROUP BY L.iUsername 
ORDER BY rank ASC
于 2012-07-26T15:10:22.623 回答
-1

试试那个查询。

Select user_id, count(user_id) from tablename group by user_id;
于 2012-07-26T15:10:31.790 回答