我的CSV看起来像这样:
Record Key Sequence Number Transportation Segment Number Arrival Date
18010312076777 1 1 01/16/2018
18010312076777 1 2 01/17/2018
18010312076867 1 1 01/16/2018
18010312076867 1 2 01/17/2018
18010312078634 1 1 01/16/2018
18010312078634 1 2 01/18/2018
18010912098476 1 1 1/10/2018
18010912098476 1 2 1/10/2018
18010912098476 1 3 1/11/2018
18010912098476 1 4 1/11/2018
18010912099327 1 1 01/16/2018
18010912099327 1 2 01/17/2018
18010912101156 1 1 01/16/2018
18010912101156 1 2 01/17/2018
我使用了Import-Csv cmdlet将其作为哈希表引入。这很好。
我尝试根据以下逻辑创建另一个哈希表:对于每个唯一的记录键和序列号组合,返回最大的运输段号及其对应的到达日期
例如,第一个记录密钥18010312076777列出两次,序列号均为1(尽管序列号可以是任何值)。最大的运输部门编号为2。
记录密钥18010912098476有四个条目,但我们只对具有运输分段编号= 4的条目感兴趣。如果此记录密钥有一个条目编号为2的条目,我们需要撤回序列号= 1 和序列号= 2的行。
所以新的哈希表应该包含:
Record Key Sequence Number Transportation Segment Number Arrival Date
18010312076777 1 2 01/17/2018
18010312076867 1 2 01/17/2018
18010312078634 1 2 01/18/2018
18010912098476 1 4 1/11/2018
18010912099327 1 2 01/17/2018
18010912101156 1 2 01/17/2018
我已经写了这段代码
function New-LastSegments {
# For each unique Record-Key + Sequence number, return the largest Transportation Segment Number and the corresponding Arrival Date
# Create a new hashtable
$global:last_segment_reference = @{};
# Get a list of unique record keys to search on
$record_keys = $global:transeg_table | Select-Object "Record Key", "Sequence Number" -Unique | Out-String;
# Step through the record keys
$record_keys | ForEach-Object ({
# New row
$new_segment_reference = @{};
# Get the last transportation segment number that matches our unique Record Key and Sequence Number
$transportation_segment_number = $global:transeg_table |
Where-Object ({$global:transeg_table.$('Record Key') -eq $_.$('Record Key')}) -and ({$global:transeg_table.$('Sequence Number') -eq $_.$('Sequence Number')}) | ForEach-Object $global:transeg_table.$('Transportation Segment Number') | Select-Object -last 1
$new_segment_reference | Add-Member -Name "Record Key" -MemberType NoteProperty -Value $_.$('Record Key');
$new_segment_reference | Add-Member -Name "Sequence Number" -MemberType NoteProperty -Value $_.$('Sequence Number');
$new_segment_reference | Add-Member -Name "Transportation Segment Number" -MemberType NoteProperty -Value $_.$transportation_segment_number;
# I think this needs to be set at the same time as the Transportation Segment Number
$new_segment_reference | Add-Member -Name "Arrival Date" -MemberType NoteProperty -Value $_.$('Arrival Date');
# Add the new row to the new hashtable
$global:last_segment_reference += $new_segment_reference;
})
}
我正在
" Where-Object:找不到与参数匹配的参数 姓名'和'。"
和
"属性'记录密钥'在这个对象上找不到。验证 该物业存在。"
任何人都可以帮助我使用逻辑和语法来生成这个哈希表吗?我只使用一个密钥就可以使用其他哈希表,但是我遇到了多个密钥的困难。
答案 0 :(得分:0)
这里我刚按升序排序前三列(Record Key
,Sequence Number
和Transportation Segment Number
)(您的示例表实际上已按此方式排序,但我选择不作出假设)。然后,我根据Record Key
和Sequence Number
对表数据进行了分组,并选择了每个组中的最后一项:
$groups = ($global:transeg_table | sort -Property "Record Key","Sequence Number","Transportation Segment Number" | Group-Object -Property "Record Key","Sequence Number")
foreach ($group in $groups) { $group.Group | select -last 1 }
Record Key Sequence Number Transportation Segment Number Arrival Date
---------- --------------- ----------------------------- ------------
18010312076777 1 2 01/17/2018
18010312076867 1 2 01/17/2018
18010312078634 1 2 01/18/2018
18010912098476 1 4 1/11/2018
18010912099327 1 2 01/17/2018
18010912101156 1 2 01/17/2018
它可能不是最优雅的解决方案,但它似乎不像你现在的那样复杂,它确实有效(假设我已正确理解问题)。