我有一组数据,其中有六行小数,我想总结一下,它们是按帐户代码分组的。我有一个存储的proc,它是将数据带入实体的视图,但我不确定按照我的逻辑。
List<NominalSums> _nominalSums = new List<NominalSums>()
foreach (var items in _nominalData)
{
using (var db = new ConsolidatedSage50Entities())
{
var accRef = items.ACCOUNT_REF;
var clientIdParameter1 = new SqlParameter("@AccountRef", SqlDbType.NVarChar);
clientIdParameter1.Value = accRef;
var clientIdParameter2 = new SqlParameter("@SortOrder", SqlDbType.NVarChar);
clientIdParameter2.Value = 321;
var clientIdParameter3 = new SqlParameter("@CATEGORY", SqlDbType.BigInt);
clientIdParameter3.Value = 5;
var result = db.Database.SqlQuery<sp_fuel_sumbalances_Result>("sumbalances @AccountRef, @SortOrder, @CATEGORY", new object[] { clientIdParameter1, clientIdParameter2, clientIdParameter3 }).ToList();
foreach (var groupData in result)
{
_nominalSums.Add(new NominalSums()
{
ACCOUNT_REF = groupData.ACCOUNT_REF,
BALANCE_MTH11 = groupData.BALANCE_MTH1,
SortOrder =(int) groupData.SORT_ORDER,
Category = (int) groupData.CATEGORY
});
}
}
}
return _nominalSums;
}
上面的代码会为每个结果打印一行新的一行,因为我只想一行结合存储的proc的结果总数,可以有人在这里向我指出正确的方向。
编辑2 我的主要问题是因为我的存储过程如下所示,我将如何对该存储过程进行组和运算呢?因为我只想从主查询中返回一行,而对于一组特定的critera,该行有六行。
ALTER PROCEDURE [dbo].[sumbalances]
-- Add the parameters for the stored procedure here
@AccountRef varchar(500),
@SortOrder int,
@CATEGORY int
as
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT NOMINAL_LEDGER.ACCOUNT_REF, NOMINAL_LEDGER.NAME, NOMINAL_LEDGER.ACCOUNT_TYPE, NOMINAL_LEDGER.BALANCE, NOMINAL_LEDGER.QUICK_RATIO, NOMINAL_LEDGER.SOFA_ID,
NOMINAL_LEDGER.PRIOR_YR_MTH1, NOMINAL_LEDGER.PRIOR_YR_MTH2, NOMINAL_LEDGER.PRIOR_YR_MTH3, NOMINAL_LEDGER.PRIOR_YR_MTH4, NOMINAL_LEDGER.PRIOR_YR_MTH5,
NOMINAL_LEDGER.PRIOR_YR_MTH6, NOMINAL_LEDGER.PRIOR_YR_MTH7, NOMINAL_LEDGER.PRIOR_YR_MTH8, NOMINAL_LEDGER.PRIOR_YR_MTH9, NOMINAL_LEDGER.PRIOR_YR_MTH10,
NOMINAL_LEDGER.PRIOR_YR_MTH11, NOMINAL_LEDGER.PRIOR_YR_MTH12, NOMINAL_LEDGER.PRIOR_YR2_MTH1, NOMINAL_LEDGER.PRIOR_YR2_MTH2, NOMINAL_LEDGER.PRIOR_YR2_MTH3,
FROM CATEGORY left JOIN
NOMINAL_LEDGER ON CATEGORY.CompanyID = NOMINAL_LEDGER.CompanyID
WHERE ACCOUNT_REF=@AccountRef AND SORT_ORDER=@SortOrder and CATEGORY=@CATEGORY
END
答案 0 :(得分:0)
这是一些sudo代码,我不确定您要总结哪些字段,我想您是用accountID说的。因此,您需要做两件事才能使此工作通过accountID取得总和:
将SUM(FieldsYouWantToSumUpByAccountID)作为FieldNameYouWantToCallIt添加到您的选择中
在我添加的“分组依据”中,您将对您希望在选择列表中进行的所有字段进行分组(未进行汇总)。 SO ID,Ref等。
t
SELECT SUM(FieldYouWantSummed), NOMINAL_LEDGER.ACCOUNT_REF, NOMINAL_LEDGER.NAME, NOMINAL_LEDGER.ACCOUNT_TYPE, NOMINAL_LEDGER.BALANCE, NOMINAL_LEDGER.QUICK_RATIO, NOMINAL_LEDGER.SOFA_ID,
NOMINAL_LEDGER.PRIOR_YR_MTH1, NOMINAL_LEDGER.PRIOR_YR_MTH2, NOMINAL_LEDGER.PRIOR_YR_MTH3, NOMINAL_LEDGER.PRIOR_YR_MTH4, NOMINAL_LEDGER.PRIOR_YR_MTH5,
NOMINAL_LEDGER.PRIOR_YR_MTH6, NOMINAL_LEDGER.PRIOR_YR_MTH7, NOMINAL_LEDGER.PRIOR_YR_MTH8, NOMINAL_LEDGER.PRIOR_YR_MTH9, NOMINAL_LEDGER.PRIOR_YR_MTH10,
NOMINAL_LEDGER.PRIOR_YR_MTH11, NOMINAL_LEDGER.PRIOR_YR_MTH12, NOMINAL_LEDGER.PRIOR_YR2_MTH1, NOMINAL_LEDGER.PRIOR_YR2_MTH2, NOMINAL_LEDGER.PRIOR_YR2_MTH3,
FROM CATEGORY
LEFT JOIN NOMINAL_LEDGER ON CATEGORY.CompanyID = NOMINAL_LEDGER.CompanyID
WHERE ACCOUNT_REF=@AccountRef
AND SORT_ORDER=@SortOrder
AND CATEGORY=@CATEGORY
GROUP BY Account_REF (AND ALL other fields that you want duplicated)
如果您可以提供要选择的字段列表和要累加的字段,如果不清楚,我可以为您更新上述声明。