У меня есть два метода:
private string CreateWeekPivotTable(string TemporaryTable, DateTime StartDate)
{
string pivotTable = "Select TT.Description, TT.Name, TT.Code, \n";
int countday = 7;
StartDate = StartDate.AddDays(-1 * (7 * 4));
for (int i = 0; i < 4; i++)
{
string week = $"Ifnull(SUM(case when TT.DateSale = '{DateFormat(StartDate.AddDays(countday),
OptionsPeriodEnum.YEAR)}' And TT.Week ='{WeekNumber(StartDate.AddDays(countday)).ToString("D" +2)}'
then TT.Quantity else 0 end), 0.0) as 'Week{(i + 1).ToString("D" + 2)}', \n ";
pivotTable += week;
countday += 7;
}
pivotTable = pivotTable + " '' as Empty \n from TemporaryTable TT \n group by
TT.Description,TT.Name,TT.Code";
return $"{TemporaryTable} {pivotTable}";
}
private int WeekNumber(DateTime Date)
{
return CultureInfo.Calendar.GetWeekOfYear(Date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
, которые дают мне следующий вывод:
Ifnull(SUM(case when TT.DateSale = '2019' And TT.Week ='53' then TT.Quantity else 0 end), 0.0) as 'Week01',
Ifnull(SUM(case when TT.DateSale = '2020' And TT.Week ='02' then TT.Quantity else 0 end), 0.0) as 'Week02',
Ifnull(SUM(case when TT.DateSale = '2020' And TT.Week ='03' then TT.Quantity else 0 end), 0.0) as 'Week03',
Ifnull(SUM(case when TT.DateSale = '2020' And TT.Week ='04' then TT.Quantity else 0 end), 0.0) as 'Week04'
Проблема в том, что она не включает неделю 01 с 2020 года. Как я могу решить это?