0

MySQL 查询

select MAX(os.aggregate_date) as lastMonthDay,os.totalYTD
  from (SELECT aggregate_date,Sum(YTD) AS totalYTD 
        FROM tbl_aggregated_tables
          WHERE subscription_type = 'Subcription Income'
        GROUP BY aggregate_date) as os
GROUP by MONTH(os.aggregate_date),YEAR(os.aggregate_date)
ORDER BY lastMonthDay;

转换为此 LINQ 查询

var income = context.tbl_aggregated_tables
                   .Where(s => s.subscription_type == "Subcription Income")
                   .GroupBy(s => s.aggregate_date)
                   .Select(result => new
                   {
                       date = result.Key,
                       ytdsum = result.Select(x => x.YTD).Sum()
                   })
                   .GroupBy(s => new { month = s.date.Month, year = s.date.Year })
                   .Select(
                    // select max data and take its ytdsum value 
                        ).ToList();

第二个分组的目的是找到每个月的最大天数。现在,如何在第二次分组后选择每个月的最大日期及其 ytdsum?

更新

income = context.tbl_aggregated_tables
                   .Where(s => s.subscription_type == "Subcription Income")
                   .GroupBy(s => s.aggregate_date)
                   .Select(result => new
                   {
                       date = result.Key,
                       ytdsum = result.Select(x => x.YTD).Sum()
                   })
                   .GroupBy(s => new { s.date.Month, s.date.Year })
                   .Select(
                     x => x.Max(s => s.date)
                   ).ToList()

这样它只返回日期,我无法返回列表的完整对象,包括 ytdSum。

4

2 回答 2

0

这工作得很好,但它比原来的 mysql 查询需要更多的时间。

 var income = context.tbl_aggregated_tables
                       .Where(s => s.subscription_type == "Subcription Income"
                        )
                       .GroupBy(s => s.aggregate_date)
                       .Select(result => new
                       {
                           date = result.Key,
                           ytdsum = result.Select(x => x.YTD).Sum()
                       })
                       .GroupBy(s => new { s.date.Month, s.date.Year })
                       .Select(
                         x => x.OrderByDescending(s => s.date)
                       )
                       .ToList()
                       .Select(el => el.FirstOrDefault())
                       .OrderBy(s=>s.date);
于 2019-12-25T12:43:30.540 回答
0

这应该有效:

var income = context.tbl_aggregated_tables
                   .Where(s => s.subscription_type == "Subcription Income")
                   .GroupBy(s => s.aggregate_date)
                   .Select(result => new
                   {
                       date = result.Key,
                       ytdsum = result.Select(x => x.YTD).Sum()
                   })
                   .GroupBy(s => new { month = s.date.Month, year = s.date.Year })
                   .Select(
                        x => x.OrderByDescending(k => k.date).First() 
                   ).ToList();

income现在是一个对象列表,每个对象都有date并且ytdsum

于 2019-12-24T11:43:49.943 回答