T-SQL 查询和按报告分组帮助

发布于 2024-08-26 07:12:55 字数 413 浏览 3 评论 0原文

所以我有一些看起来像这样的数据。

`USERID1 USERID2`
    1        10
    2        20
    2        30
    3        40
    3        50
    1        10
    2        20
    2        30
    3        50

我想要一个生成以下内容的查询

`USERID1     COUNT`
    2        2
    3        2

这是一个分组查询,它显示每个 USERID1 的唯一 USERID2 的计数,该 USERID1 有超过 1 个与之关联的 USERID2。天哪,我希望你不会像我一样对最后一句话感到困惑。

So I have some data that looks like this.

`USERID1 USERID2`
    1        10
    2        20
    2        30
    3        40
    3        50
    1        10
    2        20
    2        30
    3        50

I want a query that produces the following

`USERID1     COUNT`
    2        2
    3        2

It's a group by query that shows me the count of unique USERID2 for each USERID1 that has more than 1 USERID2 associated with it. God I hope you aren't as confused as I am by that last statement.

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

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

发布评论

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

评论(5

无所的.畏惧 2024-09-02 07:12:55

这有效吗?

SELECT USERID1, COUNT(DISTINCT USERID2) 
  FROM [table] 
 GROUP BY USERID1 
HAVING COUNT(DISTINCT USERID2) > 1

Does this work?

SELECT USERID1, COUNT(DISTINCT USERID2) 
  FROM [table] 
 GROUP BY USERID1 
HAVING COUNT(DISTINCT USERID2) > 1
人生戏 2024-09-02 07:12:55

怎么样:

select userid1, count(*) from tablename group by userid1 having count(*) > 1

How about:

select userid1, count(*) from tablename group by userid1 having count(*) > 1
π浅易 2024-09-02 07:12:55

你能尝试这样的事情吗:-

SELECT UserID1, COUNT(UserID2) FROM Table1 GROUP BY UserId1
HAVING COUNT(UserID2)>1

Could you try something like this:-

SELECT UserID1, COUNT(UserID2) FROM Table1 GROUP BY UserId1
HAVING COUNT(UserID2)>1
硪扪都還晓 2024-09-02 07:12:55

这个怎么样??

选择用户id1,计数(不同的用户id2)
来自表名
其中 userid1 > 1
按 userid1 分组

How about this??

select userid1, count(distinct userid2)
from nameOfTable
where userid1 > 1
group by userid1

绾颜 2024-09-02 07:12:55
select userid1, count(userID1) as [count] from tablename group by userid1 order by userid1
select userid1, count(userID1) as [count] from tablename group by userid1 order by userid1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文