如何编写一个SQL查询以列出不同的值及其从表中的出现数量?
我有一个包含警报事件历史记录的表。 我认为这是消息的唯一相关列,但是还有其他列,例如时间/日期和来源。
这是一个示例表:
时间/日期 | 消息 | 源 |
---|---|---|
2022-04-27/11:59:28 | 代码1 | VFD1 |
2022-04-27/11:59:37 | 代码4 | VFD1 |
2022-04-27/11:59:39 | 代码1 | VFD1 |
2022-04-27/11:59:42 | 代码 | 2 VFD1 |
2022-04-27/11:59:44 | 代码1 | VFD1 |
2022-04-27/11:59:46 | 代码3 | VFD1 |
2022-2022-04-27/11 :59:48 | 代码1 | VFD1 |
2022-04-27/11:59:50 | 代码2 | VFD1 |
,我想创建类似的内容:
消息 | 出现 |
---|---|
代码1 | 4 |
代码2 | 2 |
代码3 1 | 代码4 |
代码4 | 1 |
这是在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/Date | Message | Source |
---|---|---|
2022-04-27/11:59:28 | Code 1 | VFD1 |
2022-04-27/11:59:37 | Code 4 | VFD1 |
2022-04-27/11:59:39 | Code 1 | VFD1 |
2022-04-27/11:59:42 | Code2 | VFD1 |
2022-04-27/11:59:44 | Code 1 | VFD1 |
2022-04-27/11:59:46 | Code 3 | VFD1 |
2022-04-27/11:59:48 | Code 1 | VFD1 |
2022-04-27/11:59:50 | Code 2 | VFD1 |
From this, I'd like to create something like this:
Message | Occurrences |
---|---|
Code 1 | 4 |
Code 2 | 2 |
Code 3 | 1 |
Code 4 | 1 |
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您只需要汇总?
It appears you just need to aggregate?