如何针对我的和表编写以下LEFT OUTER JOIN
SQL 查询,以便在 LINQ 中按天、周或月对总销售额进行分组,以便它可以通过 LINQ-to-SQL 实现?Calendar
Sales
SELECT c.CalendarDate, c.FirstDayOfWeek, c.FirstDayOfMonth,
ISNULL(s.Total, 0) as Total
FROM Calendar as c
LEFT OUTER JOIN Sales as s
on s.SaleDate >= c.CalendarDateTime and
s.SaleDate < c.NextDayDateTime
WHERE s.SaleDate BETWEEN @since and @until
我设法让内部联接在 LINQ 中工作,但我需要外部联接来检索零销售额的天数。这是我用于内部连接的代码:
var sales = from s in db.Sales
from c in db.Calendars
where
s.SaleDate >= c.CalendarDate && s.SaleDate < c.NextDayDateTime
&& s.SaleDate >= sinceDate && s.SaleDate < dateEnd
select new
{
c.CalendarDate,
c.FirstDateOfWeek,
c.FirstDateOfMonth,
s.Total
};
然后我可以按如下方式打开日期间隔和分组销售:
日常的:
var groupedSales = sales.GroupBy(x => x.CalendarDate);
每周:
var groupedSales = sales.GroupBy(x => x.FirstDateOfWeek);
每月:
var groupedSales = sales.GroupBy(x => x.FirstDateOfMonth);
最后:
var salesReport = from g in groupedSales
orderby g.Key
select new {
Date = g.Key,
Total = g.Sum(x => x.Total)
};
或者,它也可以在仅检索非零天的销售后将零销售记录注入我的报告中。