使用 SqlKata 按 sql 中的月份名称分组
Group by month name in sql using SqlKata
我有以下查询,我想按 月 而不是完整日期对结果进行分组。
有什么简单的方法可以实现吗?注意我正在使用 SqlKata 库
预期结果
Month | Balance
------|--------
1 | 100
2 | 150
3 | 300
而不是
Date | Balance
-------- |------
2018-01-01 | 100
2018-01-02 | 90
..... | ....
2018-02-01 | 150
我当前的代码是:
var balances = db.Query("Balances")
.WhereBetween("Date", from, to)
.Select("Date")
.Select("sum([Balance]) as Balance")
.GroupBy("Date")
.Get();
只需这样做:
select month(`Date`);
您有 GroupByRaw ( and SelectRaw
) Just use a database built in functions to get the month. Let's supose you are working with SQL Server and MONTH 功能:
var balances = db.Query("Balances")
.WhereBetween("Date", from, to)
.SelectRaw("MONTH(Date)")
.Select("sum([Balance]) as Balance")
.GroupByRaw("MONTH(Date)")
.Get();
免责声明:检查您的数据库品牌文档中的日期函数,以了解如何从您的案例中获取日期中的月份。
我有以下查询,我想按 月 而不是完整日期对结果进行分组。 有什么简单的方法可以实现吗?注意我正在使用 SqlKata 库
预期结果
Month | Balance
------|--------
1 | 100
2 | 150
3 | 300
而不是
Date | Balance
-------- |------
2018-01-01 | 100
2018-01-02 | 90
..... | ....
2018-02-01 | 150
我当前的代码是:
var balances = db.Query("Balances")
.WhereBetween("Date", from, to)
.Select("Date")
.Select("sum([Balance]) as Balance")
.GroupBy("Date")
.Get();
只需这样做:
select month(`Date`);
您有 GroupByRaw ( and SelectRaw
) Just use a database built in functions to get the month. Let's supose you are working with SQL Server and MONTH 功能:
var balances = db.Query("Balances")
.WhereBetween("Date", from, to)
.SelectRaw("MONTH(Date)")
.Select("sum([Balance]) as Balance")
.GroupByRaw("MONTH(Date)")
.Get();
免责声明:检查您的数据库品牌文档中的日期函数,以了解如何从您的案例中获取日期中的月份。