与该列的不同值的每个记录一起的不同列的计数

发布于 2025-01-09 13:55:33 字数 584 浏览 0 评论 0原文

我是 SQL 新手,不确定这是否是正确的方法,但基本上我试图获取不同日期的计数并将其放在单个日期旁边

例如,

Date     Username Count(Distinct(Date))
20220721 jbravo    3
20220722 jbravo    3
20220723 jbravo    3
20220721 mario    7
20220722 mario    7
20220723 mario    7
20220724 mario    7 
20220725 mario    7
20220726 mario    7
20220727 mario    7

我知道如何对 count(distinct(date)) 进行分组由用户,但我如何实现以下目标,因为如果我这样做

select Date, Username, Count(Distinct(Date))
from table
group by Username

会抛出一个错误,我需要在 group by 子句中添加 Date

如果我在 group by 子句中添加 Date,显然不同计数变为 1

I am new to SQL and am not sure if this is the right approach, but basically I am trying to get the count of distinct dates and put that beside the individual date

For eg

Date     Username Count(Distinct(Date))
20220721 jbravo    3
20220722 jbravo    3
20220723 jbravo    3
20220721 mario    7
20220722 mario    7
20220723 mario    7
20220724 mario    7 
20220725 mario    7
20220726 mario    7
20220727 mario    7

I know how to get count(distinct(date)) grouped by user, but how do I achieve the below since, if I do

select Date, Username, Count(Distinct(Date))
from table
group by Username

will throw an error that I need Date in the group by clause

If I add Date in the group by clause, obviously the distinct count becomes 1

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

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

发布评论

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

评论(2

掌心的温暖 2025-01-16 13:55:33

这是一个聚合问题。您可以使用公共表表达式

WITH t1 AS (
  SELECT username, COUNT(DISTINCT(dates)) AS dist_dates
  FROM tab1
  GROUP BY username
  )
SELECT tab1.dates, tab1.username, t1.dist_dates
FROM tab1
INNER JOIN t1
ON t1.username = tab1.username

您应该知道最好的选择是加入 ids。但是,由于您没有提到您有这个,所以这个解决方案工作正常。

That is an aggregation issue. You can use common table expression

WITH t1 AS (
  SELECT username, COUNT(DISTINCT(dates)) AS dist_dates
  FROM tab1
  GROUP BY username
  )
SELECT tab1.dates, tab1.username, t1.dist_dates
FROM tab1
INNER JOIN t1
ON t1.username = tab1.username

You should be aware that the best option would be joining on ids. However, since you did not mention you have that, this solution works fine.

高冷爸爸 2025-01-16 13:55:33

GROUP BY x, y, z 表示您希望每个 x, y, z 一个结果行。 group by username 一定是错误的,因为您希望每个用户名有多个结果行。

事实上,您根本不想聚合行,而是按原样显示所有行。您只想向这些行添加聚合信息。这是通过分析函数完成的,该函数是一个聚合函数,与一个 over 子句相结合,告诉 DBMS 您希望对哪些行进行聚合评估。

您还没有告诉我们您的 DBMS。以下是标准 SQL 查询:

select
  date,
  username,
  count(distinct date) over (partition by username) as dates
from mytable
order by date, username;

GROUP BY x, y, z means you want one result row per x, y, z. group by username must be wrong, because you want more than one result row per username.

In fact you don't want to aggregate your rows at all, but show all rows as they are. You only want to add an aggregation information to these rows. This is done with an analytic function, which is an aggregation function in combination with an over clause that tells the DBMS over which rows you want the aggregation evaluation.

You haven't told us your DBMS. Here is the standard SQL query for this:

select
  date,
  username,
  count(distinct date) over (partition by username) as dates
from mytable
order by date, username;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文