使用PowerShell将两个相似的表与两个不同的表组合在一起

时间:2014-03-25 17:03:22

标签: mysql powershell

我需要加入我在Powershell中创建的两个表。问题是,在他们共享的5列中,每个表中有两列不同。如何加入这两个表,然后才能对它们进行排序。我猜我会做两件事:

实施例: 表格1 列:a,b,c,d,e

表2 列a,x,c,d,z

我是否必须为每个空表添加一个新列,但要表示另一个表中缺少的列,这样当我执行table1 + table2时,它们不会丢弃任何数据?

没有sql连接的代码示例。

    $Command.CommandText = "SELECT job_number, job_desc, permit_number, pieces, rate, postage_total FROM monticello_charges WHERE (insert_date >= '$from') AND (insert_date < '$to') ORDER BY job_number"
   $Command2.CommandText = "SELECT job_number, description, permit_number, pieces, rate, total FROM mailshop_charges WHERE (mailing_date >= '$from') AND (mailing_date < '$to')ORDER BY permit_number, job_number"

   $Reader = $Command.ExecuteReader() 
   $Reader2 = $Command2.ExecuteReader()  

   ###################################
   #Do not add entries that have a [permit_number] of (360) and [rate] less than (0) 
   ###################################
   $results = @()
   while ($Reader.Read())
   {
        $row = [ordered]@{}        
        for ($i = 0; $i -lt $Reader.FieldCount; $i++)
        {
            if($reader.GetValue(2) -eq 360 -and $reader.GetValue(4) -lt 0)
        {
              #pass over it
            }
            else
            {
               $row[$reader.GetName($i)] = $reader.GetValue($i)
            }             
        }
        $results += new-object psobject -property $row 
   }

   ###################################
   # Build the second table and replace 280 permits to (M) 
   ###################################
   $results2 = @()
   while ($Reader2.Read())
   {       
        $row = [ordered]@{}
        for ($i = 0; $i -lt $Reader2.FieldCount; $i++)
        {
            if($i -eq 2 -and $reader2.GetValue($i) -eq 280)
        {
              $row[$reader2.GetName($i)] = "M"
          #$counter++
            }
            else
            {
               $row[$reader2.GetName($i)] = $reader2.GetValue($i)
            }             
        }
        $results2 += new-object psobject -property $row            
   }


   $connection.Close();
   $connection2.Close();

   ###################################
   #Combine the tables and sort,then inject to CSV output file.
   ##################################    
   $finalResult = $results + $results2;   

   #$finalResult = $finalResult | sort-object @{Expression = {$_.permit_number}; Ascending = $true},{$_.job_number}

   $finalResult | sort-object @{Expression = {$_.permit_number}; Ascending = $true},{$_.job_number} | export-csv c:\Users\cpharr\Desktop\out.csv

   Write-Host "Report has been exported to current directoy. Filename:[out.csv]"

提前致谢!

1 个答案:

答案 0 :(得分:2)

好的,假设您没有链接条目且所有记录都是独立的,您可以这样做:

$T2Keys = $Results2|gm|?{$_.MemberType -match "Property"}|Select -ExpandProperty Name
$T1Keys = $Results|gm|?{$_.MemberType -match "Property"}|Select -ExpandProperty Name
$KeysToAdd = $T2Keys|?{$T1Keys -notcontains $_}
$Results3 = @()
$Results3 += $Results
$KeysToAdd|%{$Results3|Add-Member $_ ""}
$Results3+=$Results2

将第一个表的属性查询为变量。它与第二个表格相同。它找到第二个表中不属于第一个表的属性,并将它们保存到变量中。然后它创建一个空数组,向其添加第一个表,添加缺少的字段,然后将第二个表添加到其中。