English 中文(简体)
按组别分列的小组完成后记录数目
原标题:Calculating the number of records after doing a GROUP BY in SQL

是否有办法计算已经拥有<代码>的一套记录中的记录数目。 适用的组别

I have a table that contains a weekly summary of wins, ties and losses for all users in a series of games. It looks sort of like this:

userid  weekid  wins  losses  ties
1       1       10    5       2
1       2       11    3       3
1       3       8     9       0
2       1       11    6       0
2       2       7     9       1
2       3       9     7       1
3       1       3     14      0
3       2       7     9       1
3       3       6     9       2

I want to determine the rank of a given user. I would do this by counting the number of users with a better combination of wins, ties and losses than that user. If I wanted to determine the rank of user #3 for example, I could use this query, but it only gets me half way there:

SELECT COUNT(userid) 
FROM scores
GROUP BY userid
HAVING (SUM(wins) * 2) + SUM(ties) > 35

结果是:

COUNT(userid)
3
3

这使我感到有记录,有2个记录(他排名第三)。 我真的喜欢的是记录数目(2)而不是记录本身。 我认为的唯一事情是把记录放在临时桌上,并在那里打上<条形码>。 或者,我可以把数据转回C#,并记录在那里,但如果特定用户有可怕的记录,我需要发送大量记录。

我希望能以相对廉价的方式做到这一点。

最佳回答
SELECT COUNT(*) 
FROM
(
    SELECT (SUM(wins) * 2) + SUM(ties) as Rank
    FROM scores
    GROUP BY userid
) tt
WHERE Rank > 35
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签