如何将2个不同组的值输出到一行上?

时间:2019-04-02 00:32:53

标签: xslt sum grouping

我有一个员工档案,每个工资期收到不同类型的收入。每个员工可以有1到3种不同的收入标记类型(A,B,C),这些收入可以在不同的日期使用。

对于每位员工,如果记录具有相同的收入标记和日期,我希望将金额总计。如果说B没有收入,那么B位置应该有0。

我希望输出尽可能在同一行上,以使可读性和验证更加容易。这是我遇到的麻烦。

我已按EmployeeID,EarningFlag和Date分组。然后,我汇总了每个分组的金额。尽管输出在单独的行上,并且很混乱。我不知道如果员工在该日期没有这样的收入标记,如何将零置位。

示例XML:

<Entry>
    <EmployeeName>Bob Stevens</EmployeeName>
    <EmployeeID>123</EmployeeID>
    <EarningFlag>A</EarningFlag>
    <Date>2019-04-01</Date>
    <Amount>2031.54</Amount>
</Entry>
<Entry>
    <EmployeeName>Bob Stevens</EmployeeName>
    <EmployeeID>123</EmployeeID>
    <EarningFlag>A</EarningFlag>
    <Date>2019-04-01</Date>
    <Amount>30.74</Amount>
</Entry>
<Entry>
    <EmployeeName>Bob Stevens</EmployeeName>
    <EmployeeID>123</EmployeeID>
    <EarningFlag>B</EarningFlag>
    <Date>2019-04-01</Date>
    <Amount>1.63</Amount>
</Entry>
<Entry>
    <EmployeeName>Samantha Philips</EmployeeName>
    <EmployeeID>036</EmployeeID>
    <EarningFlag>C</EarningFlag>
    <Date>2019-04-01</Date>
    <Amount>631.54</Amount>
</Entry>
<Entry>
    <EmployeeName>Samantha Philips</EmployeeName>
    <EmployeeID>036</EmployeeID>
    <EarningFlag>C</EarningFlag>
    <Date>2019-04-01</Date>
    <Amount>3771.33</Amount>
</Entry>
<Entry>
    <EmployeeName>Samantha Philips</EmployeeName>
    <EmployeeID>036</EmployeeID>
    <EarningFlag>A</EarningFlag>
    <Date>2019-04-01</Date>
    <Amount>631.54</Amount>
</Entry>
<Entry>
    <EmployeeName>Samantha Philips</EmployeeName>
    <EmployeeID>036</EmployeeID>
    <EarningFlag>B</EarningFlag>
    <Date>2019-03-07</Date>
    <Amount>3771.33</Amount>
</Entry>
<xsl:for-each-group select="Entry" group-by="concat(EmployeeID,Date,EarningFlag)">

   <xsl:sort select="current-grouping-key()"/>  

   <xsl:value-of select="EmployeeID"/>
   <xsl:text>,</xsl:text>

   <xsl:value-of select="EmployeeName"/>
   <xsl:text>,</xsl:text>

   <xsl:value-of select="Date"/>
   <xsl:text>,</xsl:text>

   <xsl:if test="EarningFlag='A'">
    <xsl:value-of select="sum(current-group()/Amount)"/>
   </xsl:if>
   <xsl:text>,</xsl:text>
   <xsl:if test="EarningFlag='B'">
    <xsl:value-of select="sum(current-group()/Amount)"/>
   </xsl:if>
   <xsl:text>,</xsl:text>
   <xsl:if test="EarningFlag='C'">
    <xsl:value-of select="sum(current-group()/Amount)"/>
   </xsl:if>

</xsl:for-each-group>

My actual results are:
123,Bob Stevens,2019-04-04,2062.28123,Bob Stevens,2019-04-04,1.63,
036,Samantha Philips,2019-03-07,3771.33,
036,Samantha Philips,2019-04-01,631.54,036,Samantha Philips,2019-04-01,3771.33

The expected output would be in the order of Employee ID, Employee Name, Date, A, B, C.

123,Bob Stevens,2019-04-01,2062.28,1.63,0
036,Samantha Philips,2019-03-07,0,3771.33,0
036,Samantha Philips,2019-04-01,631.54,0,3771.33


Is this even possible???

Thank you for any help.

2 个答案:

答案 0 :(得分:1)

根据@ michael.hor257k的回答,您还可以编写

 <xsl:for-each-group select="Entry" group-by="concat(EmployeeID,Date)">

    <xsl:sort select="EmployeeID"/>  
    <xsl:sort select="Date"/>  

    <xsl:value-of select="EmployeeID, EmployeeName, Date,
                          sum(current-group([EarningFlag='A']/Amount),
                          sum(current-group()[EarningFlag='B']/Amount,
                          sum(current-group()[EarningFlag='C']/Amount"
                  separator=","/>
    <xsl:text>&#10;</xsl:text>

</xsl:for-each-group>

答案 1 :(得分:0)

我想你想做

<xsl:for-each-group select="Entry" group-by="concat(EmployeeID,Date)">

    <xsl:sort select="EmployeeID"/>  
    <xsl:sort select="Date"/>  

    <xsl:value-of select="EmployeeID"/>
    <xsl:text>,</xsl:text>

    <xsl:value-of select="EmployeeName"/>
    <xsl:text>,</xsl:text>

    <xsl:value-of select="Date"/>
    <xsl:text>,</xsl:text>

    <xsl:value-of select="sum(current-group()[EarningFlag='A']/Amount)"/>
    <xsl:text>,</xsl:text>

    <xsl:value-of select="sum(current-group()[EarningFlag='B']/Amount)"/>
    <xsl:text>,</xsl:text>

    <xsl:value-of select="sum(current-group()[EarningFlag='C']/Amount)"/>
    <xsl:text>&#10;</xsl:text>

</xsl:for-each-group>