C# SUM()和AVERAGE()用CAST包装
我有一个表C# SUM()和AVERAGE()用CAST包装,c#,tsql,nhibernate,fluent-nhibernate,C#,Tsql,Nhibernate,Fluent Nhibernate,我有一个表事务。它有点大,所以有一部分: CREATE TABLE [dbo].[TRANSACTIONS] ( [transaction_ID] UNIQUEIDENTIFIER NOT NULL, [transaction_number] INT IDENTITY (1000, 1) NOT NULL, [type_id] INT NOT
事务
。它有点大,所以有一部分:
CREATE TABLE [dbo].[TRANSACTIONS]
(
[transaction_ID] UNIQUEIDENTIFIER NOT NULL,
[transaction_number] INT IDENTITY (1000, 1) NOT NULL,
[type_id] INT NOT NULL,
[source_currency_ID] INT NOT NULL,
[target_currency_ID] INT NOT NULL,
[source_value] NUMERIC (14, 6) NOT NULL,
[target_value] NUMERIC (14, 6) NOT NULL,
[user_account_source] NVARCHAR (500) NULL,
[user_account_target] NVARCHAR (500) NULL,
[user_wmid] VARCHAR (12) NULL,
[user_email] NVARCHAR (51) NULL,
[create_date] DATETIME CONSTRAINT [DF__transacti__creat__589C25F3] DEFAULT (getdate()) NOT NULL,
[card_ID] INT NULL,
[payment_ID] NVARCHAR (80) NULL,
[enable_automation] BIT CONSTRAINT [DF__transacti__enabl__3AA1AEB8] DEFAULT ((1)) NOT NULL,
[client_ID] UNIQUEIDENTIFIER NULL,
[partner_ID] INT NULL,
[payment_type_ID] INT NOT NULL,
[language_ID] INT NULL,
[member_id] NUMERIC (18) NULL,
[desk_id] INT NULL,
[source_member_discount] NUMERIC (18, 3) NULL,
[member_scores] INT NULL,
[source_eps_fee_from_customer] NUMERIC (18, 6) NULL,
[source_eps_fee] NUMERIC (18, 6) NOT NULL,
[source_eps_fee_actual] NUMERIC (18, 6) NULL,
[target_eps_fee] NUMERIC (18, 6) NOT NULL,
[target_eps_fee_actual] NUMERIC (18, 6) NULL,
[target_exchanger_fee] NUMERIC(18, 6) NOT NULL DEFAULT 0,
[exchange_rate] NUMERIC (20, 9) NULL,
[source_master] BIT NULL,
[manual_rate] NUMERIC (20, 9) NULL,
[cb_cross_rate] NUMERIC (20, 9) NULL,
[source_profit] NUMERIC (18, 6) NULL,
[partner_reward] NUMERIC (18, 6) NULL,
[source_service_profit] NUMERIC (18, 6) NULL,
[is_individual] BIT NULL,
[member_discount] NUMERIC (18) NULL,
[scores] INT NULL,
[return_url] VARCHAR (300) NULL,
[err_return_url] VARCHAR (300) NULL,
[add_scores] BIT NULL,
[mm_transaction_id] NVARCHAR (80) NULL,
[lp_transaction_id] NVARCHAR (50) NULL,
[masterbank_auth_ID] NVARCHAR (50) NULL,
[cardtype] NVARCHAR (50) NULL,
[cl_num] INT NULL,
[cl_date] NVARCHAR (50) NULL,
[limit_check_day] BIT NULL,
[limit_check_month] BIT NULL,
[rapida_temp] NVARCHAR (50) NULL,
[ecard_id] BIGINT NULL,
[card_payment_complete] VARCHAR (4) CONSTRAINT [DF_TRANSACTIONS_card_payment_complete] DEFAULT ('NO') NULL,
[profit_in_rub] NUMERIC (18, 4) NULL,
[cb_rub_rate] NUMERIC (18, 4) NULL,
[check_wmid] VARCHAR (12) NULL,
[partner_rate] NUMERIC (18, 4) NULL,
[wm_desc] VARCHAR (500) NULL,
[without_real] BIT NULL,
[netex_point] UNIQUEIDENTIFIER NULL,
[secret_key] INT NULL,
[miniport_transfer] BIT NULL,
[miniport_hold] BIT NULL,
[hold_time] DATETIME NULL,
[is_from_widget] BIT NULL,
[partner_reward_currency_id] INT NULL,
[user_phone] NVARCHAR (50) NULL,
[project_ID] INT NOT NULL DEFAULT 2,
...
我通过选择从中合成所需数据:
return query
.Where(x => x.CurrentStatus == TransactionStatus.PaymentSent)
.GroupBy(x => new { SourceCurrency = x.SourceCurrency, TargetCurrency = x.TargetCurrency })
.Select(x => new ProfitabilityReportEntry
{
SourceCurrencyCode = x.Key.SourceCurrency,
TargetCurrencyCode = x.Key.TargetCurrency,
TransactionsCount = x.LongCount(),
TotalProfitRub = x.Sum(y => y.ProfitInRub),
AverageProfitRub = x.Average(y => y.ProfitInRub),
AverageAmountRub = x.Average(y => y.SourceValue * y.CbRubRate),
TotalAmountRub = x.Sum(y => y.SourceValue * y.CbRubRate)
})
.ToList();
类别ProfitabilityReportEntry
为:
public class ProfitabilityReportEntry
{
public PaymentCurrencyCode SourceCurrencyCode { get; set; }
public PaymentCurrencyCode TargetCurrencyCode { get; set; }
public long TransactionsCount { get; set; }
public decimal? AverageAmountRub { get; set; }
public decimal? AverageProfitRub { get; set; }
public decimal? TotalAmountRub { get; set; }
public decimal? AmountTurnoverPercent { get; set; }
public decimal? TotalProfitRub { get; set; }
public decimal? ProfitPercent { get; set; }
}
在映射类Transaction
(toTRANSACTIONS
)中,上述查询中求和和和平均值的属性也都是十进制的
我的查询结果如下SQL语句所示:
select transactio0_.source_currency_ID as col_0_0_, transactio0_.target_currency_ID as col_1_0_,
cast(count(*) as BIGINT) as col_2_0_,
cast(sum(transactio0_.moneyback_profit_rub) as DECIMAL(19,5)) as col_3_0_,
cast(avg(transactio0_.moneyback_profit_rub) as DECIMAL(19,5)) as col_4_0_,
cast(avg(transactio0_.source_value*transactio0_.cb_rub_rate) as DECIMAL(19,5)) as col_5_0_,
cast(sum(transactio0_.source_value*transactio0_.cb_rub_rate) as DECIMAL(19,5)) as col_6_0_
from TRANSACTIONS transactio0_
where transactio0_.change_date>? and transactio0_.change_date<? and transactio0_.status_id=? group by transactio0_.source_currency_ID, transactio0_.target_currency_ID
选择transactio0.source\u currency\u ID作为列0\u 0,选择transactio0.target\u currency\u ID作为列1\u 0,
强制转换(计数(*)为BIGINT)为col_2_0,
以十进制(19,5)表示的金额(交易金额、返款金额、利润金额)为第3列,
以十进制(19,5)表示的平均(交易回款和利润)为列4,0,
转换(平均值(transactio0.source\u值*transactio0.cb\u rub\u率)为十进制(19,5))为列5\u 0,
转换(总和(transactio0.source\u值*transactio0.cb\u摩擦率)为十进制(19,5))为列6\u 0\u
从事务处理0\u
其中transactio0.change\u date>?和交易0。更改日期例如源值
具有类型数值(14,6)
和cb摩擦率
具有类型数值(18,4)
。那么,您希望在源值*cb\u rub\u rat
的结果中得到什么类型的结果呢
我认为,您应该使文件类型彼此相等
但我认为这并不影响表现。所有CAST
运算符都用于结果表达式,因此它只执行一次。非常感谢,现在当您指出这一点时,它似乎有点明显。