2014-11-03 58 views
0

比方說,我有一個數據集:如何在一個FetchXML腳本中獲得2個聚合函數?

+--------+-------+----------+ 
| Animal | Price | FoodCost | 
+--------+-------+----------+ 
|  1 | 23 |  22 | 
|  1 | 32 |  33 | 
|  1 |  7 |  69 | 
|  2 | 45 |  55 | 
|  2 | 432 |  82 | 
|  2 | 33 |  34 | 
|  3 | 67 |  44 | 
|  5 | 671 |  62 | 
|  8 | 234 |  43 | 
+--------+-------+----------+ 

,我在我的表矩陣尋找的結果是:

+--------+-------+----------+ 
| Animal | Price | FoodCost | 
+--------+-------+----------+ 
|  1 | 62 |  124 | 
|  2 | 510 |  171 | 
|  3 | 67 |  44 | 
|  5 | 671 |  62 | 
|  8 | 234 |  43 | 
+--------+-------+----------+ 

我知道該怎麼辦1個骨料,但我會怎麼做2個骨料,即將每個動物的價格和食品成本加起來?

回答

1

SQL:

select Animal, sum(Price) as Sum_Price, sum(FoodCost) as sum_foodcost 
from dbo.AggegateTest 
group by Animal 

生成FetchXML:http://sql2fetchxml.com/

<fetch aggregate="true" mapping="logical"> 
    <entity name="AggegateTest"> 
    <attribute name="Animal" alias="AggegateTest1.Animal" /> 
    <attribute name="price" alias="Sum_Price" aggregate="sum" /> 
    <attribute name="foodcost" alias="sum_foodcost" aggregate="sum" /> 
    <attribute name="Animal" alias="Animal" groupby="true" /> 
    </entity> 
</fetch>