如何编写一个SQL查询以列出不同的值及其从表中的出现数量?

发布于 2025-01-24 12:20:31 字数 1315 浏览 0 评论 0原文

我有一个包含警报事件历史记录的表。 我认为这是消息的唯一相关列,但是还有其他列,例如时间/日期和来源。

这是一个示例表:

时间/日期消息
2022-04-27/11:59:28代码1VFD1
2022-04-27/11:59:37代码4VFD1
2022-04-27/11:59:39代码1VFD1
2022-04-27/11:59:42代码2 VFD1
2022-04-27/11:59:44代码1VFD1
2022-04-27/11:59:46代码3VFD1
2022-2022-04-27/11 :59:48代码1VFD1
2022-04-27/11:59:50代码2VFD1

,我想创建类似的内容:

消息出现
代码14
代码22
代码3 1代码4
代码41

这是在SCADA软件包(Iconics/Genesis64)内完成,因此我不确定SQL的确切风味,但我认为它应该是Microsoft SQL Server或类似的SQL。

我可以运行此操作:

SELECT COUNT( DISTINCT Message) as Messages FROM dm_Alarms

要获得我拥有多少个唯一值,但是我坚持如何计算每个唯一值,然后列出它们。

而且我不知道我可能拥有的所有价值观都会有什么消息,它可能会随着时间的流逝而变化。

谢谢

I have a table that contains a history of alarm events.
I think the only pertinent column for this is Message, but there are others such as Time/Date and Source.

Here's a sample table:

Time/DateMessageSource
2022-04-27/11:59:28Code 1VFD1
2022-04-27/11:59:37Code 4VFD1
2022-04-27/11:59:39Code 1VFD1
2022-04-27/11:59:42Code2VFD1
2022-04-27/11:59:44Code 1VFD1
2022-04-27/11:59:46Code 3VFD1
2022-04-27/11:59:48Code 1VFD1
2022-04-27/11:59:50Code 2VFD1

From this, I'd like to create something like this:

MessageOccurrences
Code 14
Code 22
Code 31
Code 41

This is being done inside a SCADA software package (ICONICS/Genesis64), so I'm not sure of the exact flavor of SQL, but I think it should be Microsoft SQL Server or similar to it.

I can run this:

SELECT COUNT( DISTINCT Message) as Messages FROM dm_Alarms

to get how many unique values I have, but I'm stuck on how to count for each unique value, and then list them.

And I do NOT know what all values I will possibly have for Message, it could be very many and change over time.

Thank You

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

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

发布评论

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

评论(1

梦里的微风 2025-01-31 12:20:31

看来您只需要汇总?

select Message, count(*) Occurrences
from dm_Alarms
group by Message;

It appears you just need to aggregate?

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