sql中如何计算计数?

发布于 2024-08-07 05:40:17 字数 416 浏览 5 评论 0原文

我有下表:

memberid  
2
2
3
4
3

...我想要以下结果:

memberid    count
2           2
3           1    ---Edit by gbn: do you mean 2?
4           1

我试图使用:

  SELECT MemberID, 
         COUNT(MemberID) 
    FROM YourTable 
GROUP BY MemberID

...但现在我想找到哪个记录具有最大计数。 IE:

memberid   count
2          2

I have the following table:

memberid  
2
2
3
4
3

...and I want the following result:

memberid    count
2           2
3           1    ---Edit by gbn: do you mean 2?
4           1

I was attempting to use:

  SELECT MemberID, 
         COUNT(MemberID) 
    FROM YourTable 
GROUP BY MemberID

...but now I want find which record which has maximum count. IE:

memberid   count
2          2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

优雅的叶子 2024-08-14 05:40:18

这应该可以实现不需要子选择的技巧:

select top 1 memberid, COUNT(*) as counted 
from members
group by memberid
order by counted desc

This should do the trick with no subselects required:

select top 1 memberid, COUNT(*) as counted 
from members
group by memberid
order by counted desc
柒七 2024-08-14 05:40:18

可以很容易地完成:

SELECT TOP 1 MemberId, COUNT(*) FROM YourTable GROUP BY MemberId ORDER By 2 DESC

Can be done quite easy:

SELECT TOP 1 MemberId, COUNT(*) FROM YourTable GROUP BY MemberId ORDER By 2 DESC
生来就爱笑 2024-08-14 05:40:18

我相信原始发帖者要求 2 个结果集。

我知道获得此信息的唯一方法(在 SQL Server 中)是将原始记录转储到临时表中,然后对其执行 SELECT 和 MAX。我确实欢迎需要更少代码的答案!

-- Select records into a temp table
SELECT
    Table1.MemberId
    ,CNT = COUNT(*)
INTO #Temp
FROM YourTable AS Table1
GROUP BY Table1.MemberId
ORDER BY Table1.MemberId

-- Get original records
SELECT * FROM #Temp

-- Get max. count record(s) 
SELECT 
    Table1.MemberId
    ,Table1.CNT
FROM #Temp AS Table1
INNER JOIN (
    SELECT CNT = MAX(CNT)
    FROM #Temp
) AS Table2 ON Table2.CNT = Table1.CNT

-- Cleanup 
DROP TABLE #Temp

I believe the original poster requested 2 result sets.

The only way I know of to get this (in SQL Server) is to dump the original records into a temp table and then do a SELECT and MAX on that. I do welcome an answer that requires less code!

-- Select records into a temp table
SELECT
    Table1.MemberId
    ,CNT = COUNT(*)
INTO #Temp
FROM YourTable AS Table1
GROUP BY Table1.MemberId
ORDER BY Table1.MemberId

-- Get original records
SELECT * FROM #Temp

-- Get max. count record(s) 
SELECT 
    Table1.MemberId
    ,Table1.CNT
FROM #Temp AS Table1
INNER JOIN (
    SELECT CNT = MAX(CNT)
    FROM #Temp
) AS Table2 ON Table2.CNT = Table1.CNT

-- Cleanup 
DROP TABLE #Temp
作妖 2024-08-14 05:40:18

这个查询怎么样:

SELECT TOP 1 MemberID, 
       COUNT(MemberID) 
FROM YourTable 
GROUP BY MemberID
ORDER by count(MemberID) desc

How about this query:

SELECT TOP 1 MemberID, 
       COUNT(MemberID) 
FROM YourTable 
GROUP BY MemberID
ORDER by count(MemberID) desc
柳若烟 2024-08-14 05:40:18

选择计数(列名)
来自你的表;

SELECT count(column_name)
FROM your_table;

臻嫒无言 2024-08-14 05:40:18

您需要使用子选择:

SELECT MemberID, MAX(Count) FROM
    (SELECT MemberID, COUNT(MemberID) Count FROM YourTable GROUP BY MemberID)
GROUP BY MemberID

需要第二个分组依据来返回计数和 MemberID。

You need to use a subselect:

SELECT MemberID, MAX(Count) FROM
    (SELECT MemberID, COUNT(MemberID) Count FROM YourTable GROUP BY MemberID)
GROUP BY MemberID

The second group by is needed to return both, the count and the MemberID.

居里长安 2024-08-14 05:40:17
SELECT memberid, COUNT(*) FROM TheTable GROUP BY memberid

尽管如此,它不会满足您想要的输出,因为您有两次“memberid = 3”。

编辑:在问题更新后...

SELECT TOP 1 WITH TIES    --WITH TIES will pick up "joint top". 
    memberid, COUNT(*)
FROM
    TheTable 
GROUP BY 
    memberid
ORDER BY
    COUNT(*) DESC
SELECT memberid, COUNT(*) FROM TheTable GROUP BY memberid

Although, it won't work for your desired output because you have "memberid = 3" twice.

Edit: After late update to question...

SELECT TOP 1 WITH TIES    --WITH TIES will pick up "joint top". 
    memberid, COUNT(*)
FROM
    TheTable 
GROUP BY 
    memberid
ORDER BY
    COUNT(*) DESC
留蓝 2024-08-14 05:40:17
SELECT MemberID, COUNT(MemberID) FROM YourTable GROUP BY MemberID
SELECT MemberID, COUNT(MemberID) FROM YourTable GROUP BY MemberID
可爱暴击 2024-08-14 05:40:17

如果最大值并列(或更多)怎么办?您想显示其中一项还是全部?

这就是我要做的事情,

SELECT memberid, COUNT(1)
FROM members
GROUP BY memberid
HAVING COUNT(1) = (
            SELECT MAX(result.mem_count)
            FROM (  
                SELECT memberid, COUNT(1) as mem_count
                FROM members
                GROUP BY memberid
            ) as result
          )

但我希望看到一种更有效的方法。

What if there is a tie (or more) for the max? Do you want to display one or all?

This is how I would do this

SELECT memberid, COUNT(1)
FROM members
GROUP BY memberid
HAVING COUNT(1) = (
            SELECT MAX(result.mem_count)
            FROM (  
                SELECT memberid, COUNT(1) as mem_count
                FROM members
                GROUP BY memberid
            ) as result
          )

I would love to see a more efficient approach though.

梦毁影碎の 2024-08-14 05:40:17

这样做:

SELECT memberid, COUNT(memberid) AS [count] FROM [Table] GROUP BY memberid

Do it like this:

SELECT memberid, COUNT(memberid) AS [count] FROM [Table] GROUP BY memberid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文