powershell - 按多列进行计数和分组

标签 powershell count group-by

我有一个包含以下列的 CSV 文件:

  • 错误_ID
  • 日期
  • hh(两位数的小时)
  • 错误描述

它看起来像这样:

screenshot

在 SQL 中这非常简单:

SELECT X,Y,Count(1)
FROM #Table
GROUP BY X,Y

在 PowerShell 中,情况稍有不同。

最佳答案

Group-Object cmdlet 允许按多个属性进行分组:

Import-Csv 'C:\path\to\your.csv' | Group-Object ErrorID, Date

这会给你这样的结果:

Count Name          Group
----- ----          -----
    3 1, 15/07/2016 {@{ErrorID=1; Date=15/07/2016; Hour=16}, @{ErrorID=1; Da...
    1 2, 16/07/2016 {@{ErrorID=2; Date=16/07/2016; Hour=9}}

However, to display grouped values in tabular form like an SQL query would do you need to extract them from the groups with calculated properties:

Import-Csv 'C:\path\to\your.csv' | Group-Object ErrorID, Date |
    Select-Object @{n='ErrorID';e={$_.Group[0].ErrorID}},
                  @{n='Date';e={$_.Group[0].Date}}, Count

这将产生如下输出:

ErrorID Date       Count
------- ----       -----
1       15/07/2016     3
2       16/07/2016     1

关于powershell - 按多列进行计数和分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559425/

相关文章:

powershell - 从所有通讯组中删除所有前雇员

java - 程序将返回出现的情况

mysql - 表与多个 group_concat 连接

python - 返回字典 Python 中所有值的总和

php - 错误回波计数

python - 与 Pandas 中的 Groupby 滚动相关

SQL:在已分组的表上使用 CASE

powershell - 修复 PowerShell 错误 "execution of scripts is disabled on this system"的替代方法

powershell - 停止 PowerShell 管道,确保调用 end

powershell - 仅当注册表中的 DWORD32 值已存在时才为其设置值 |电源外壳