实体Sql Group By问题,请帮忙

发布于 2024-08-27 19:55:21 字数 1386 浏览 1 评论 0原文

请帮助我完成这个简单的 E-sql 查询:

var qStr = "SELECT SqlServer.Month(o.DatePaid) as month, SqlServer.Sum(o.PaidMoney) as PaidMoney FROM XACCModel.OrdersIncomes as o group by SqlServer.Month(o.DatePaid)";

这就是我所拥有的。

我有一个名为 OrdersIncomes 的简单实体,具有 ID,PaidMoney,DatePaid,Order_ID 属性,

我想像这样选择 Month 和 Summed PaidMoney:

month  Paidmoney

1 500

2 700

3 1200

T-SQL looks like this and works fine:

    select MONTH(o.DatePaid), SUM(o.PaidMoney)
    from OrdersIncomes as o
    group by MONTH(o.DatePaid)

results:
-----------

3           31.0000

4           127.0000

5           20.0000

(3 row(s) affected)

但 E-SQL 不起作用,我不知道该怎么办。这里我的 E-SQL 需要重构:

var qStr = "SELECT SqlServer.Month(o.DatePaid) as month, SqlServer.Sum(o.PaidMoney) as PaidMoney FROM XACCModel.OrdersIncomes as o group by SqlServer.Month(o.DatePaid)";

有一个异常:ErrorDescription =“标识符‘o’无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。”

如果我在 group by 子句中包含 o,例如:FROM XACCModel.OrdersIncomes as o group by o 那么我就得不到总结和汇总的结果。 这是一个错误,还是我做错了什么?

这是一个 Linq to Entities 查询,它也可以工作:

var incomeResult = from ic in _context.OrdersIncomes
     group ic by ic.DatePaid.Month into gr
     select new { Month = gr.Key, PaidMoney = gr.Sum(i => i.PaidMoney) };

help me please with this simple E-sql query:

var qStr = "SELECT SqlServer.Month(o.DatePaid) as month, SqlServer.Sum(o.PaidMoney) as PaidMoney FROM XACCModel.OrdersIncomes as o group by SqlServer.Month(o.DatePaid)";

Here's what I have.

I have simple Entity called OrdersIncomes with ID,PaidMoney,DatePaid,Order_ID properties

I want to select Month and Summed PaidMoney like this:

month  Paidmoney

1 500

2 700

3 1200

T-SQL looks like this and works fine:

    select MONTH(o.DatePaid), SUM(o.PaidMoney)
    from OrdersIncomes as o
    group by MONTH(o.DatePaid)

results:
-----------

3           31.0000

4           127.0000

5           20.0000

(3 row(s) affected)

but E-SQL doesnot work and I dont know what to do. here my E-SQL which needs refactoring:

var qStr = "SELECT SqlServer.Month(o.DatePaid) as month, SqlServer.Sum(o.PaidMoney) as PaidMoney FROM XACCModel.OrdersIncomes as o group by SqlServer.Month(o.DatePaid)";

there's an exception: ErrorDescription = "The identifier 'o' is not valid because it is not contained either in an aggregate function or in the GROUP BY clause."

if I include o in group by clause, like: FROM XACCModel.OrdersIncomes as o group by o
then I don't get summed and aggregated results.
Is this a bug, or what I'm doing wrong?

Here's a Linq to Entities query and it works too:

var incomeResult = from ic in _context.OrdersIncomes
     group ic by ic.DatePaid.Month into gr
     select new { Month = gr.Key, PaidMoney = gr.Sum(i => i.PaidMoney) };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追风人 2024-09-03 19:55:21

为 Group By 子句指定一个别名,并在 select 子句中使用该别名。

选择 PaidMonth,SqlServer.Sum(o.PaidMoney) 作为 PaidMoney
FROM XACCModel.OrdersIncomes 作为 o
按 SqlServer.Month(o.DatePaid) 分组为 PaidMonth

Give an alias name to the Group By clause and use that alias in the select clause.

SELECT PaidMonth, SqlServer.Sum(o.PaidMoney) as PaidMoney
FROM XACCModel.OrdersIncomes as o
group by SqlServer.Month(o.DatePaid) as PaidMonth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文