SQL не воспроизводится, когда Select Max(v) FROM VALUES(....) as value(v)
вложено в оператор выбора, как я делал выше. К сожалению, мне пришлось разбить их на отдельные части и объединить их во временные таблицы. Это не самое элегантное решение, но вполне может быть единственным решением без объединений. См. Приведенный ниже код для генерации столбцов.
Кроме того, при преобразовании дат из Excel в SQL предусмотрена корректировка на 2 дня.
Это объясняется далее здесь :
select *,
(1.000000000000000*(SELECT MIN(w)
FROM (VALUES (DATEDIFF(d,'1899-12-30',[EVAL])),
(CASE WHEN [Period Indicator]='N'
THEN DATEDIFF(d,'1899-12-30',[Period End])
ELSE (DATEDIFF(d,'1899-12-30',DATEADD(DAY,-1,[Period End])))
END),
(DATEDIFF(d,'1899-12-30',DATEFROMPARTS([Yr1],'12','31')))
) as value(w))) as 'Yr1 Earn % a',
(1.000000000000000*(SELECT MAX(x)
FROM (VALUES (DATEDIFF(d,'1899-12-30',DATEFROMPARTS([Yr1]-1,'12','31'))),
(DATEDIFF(d,'1900-01-01',DATEADD(DAY,-1,[Period Start]))))
as value(x))) as 'Yr1 Earn % b',
(1.000000000000000*(SELECT MIN(w)
FROM (VALUES (DATEDIFF(d,'1899-12-30',[EVAL])),
(CASE WHEN [Period Indicator]='N'
THEN DATEDIFF(d,'1899-12-30',[Period End])
ELSE (DATEDIFF(d,'1899-12-30',DATEADD(DAY,-1,[Period End])))
END),
(DATEDIFF(d,'1899-12-30',DATEFROMPARTS([Yr2],'12','31')))
) as value(w))) as 'Yr2 Earn % a',
(1.000000000000000*(SELECT MAX(x)
FROM (VALUES (DATEDIFF(d,'1899-12-30',DATEFROMPARTS([Yr2]-1,'12','31'))),
(DATEDIFF(d,'1900-01-01',DATEADD(DAY,-1,[Period Start]))))
as value(x))) as 'Yr2 Earn % b',
(1.000000000000000*(SELECT MIN(w)
FROM (VALUES (DATEDIFF(d,'1899-12-30',[EVAL])),
(CASE WHEN [Period Indicator]='N'
THEN DATEDIFF(d,'1899-12-30',[Period End])
ELSE (DATEDIFF(d,'1899-12-30',DATEADD(DAY,-1,[Period End])))
END),
(DATEDIFF(d,'1899-12-30',DATEFROMPARTS([Yr3],'12','31')))
) as value(w))) as 'Yr3 Earn % a',
(1.000000000000000*(SELECT MAX(x)
FROM (VALUES (DATEDIFF(d,'1899-12-30',DATEFROMPARTS([Yr3]-1,'12','31'))),
(DATEDIFF(d,'1900-01-01',DATEADD(DAY,-1,[Period Start]))))
as value(x))) as 'Yr3 Earn % b' ,
(1.000000000000000*(SELECT MIN(w)
FROM (VALUES (DATEDIFF(d,'1899-12-30',[EVAL])),
(CASE WHEN [Period Indicator]='N'
THEN DATEDIFF(d,'1899-12-30',[Period End])
ELSE (DATEDIFF(d,'1899-12-30',DATEADD(DAY,-1,[Period End])))
END),
(DATEDIFF(d,'1899-12-30',DATEFROMPARTS([Yr4],'12','31')))
) as value(w))) as 'Yr4 Earn % a',
(1.000000000000000*(SELECT MAX(x)
FROM (VALUES (DATEDIFF(d,'1899-12-30',DATEFROMPARTS([Yr4]-1,'12','31'))),
(DATEDIFF(d,'1900-01-01',DATEADD(DAY,-1,[Period Start]))))
as value(x))) as 'Yr4 Earn % b',
(1.000000000000000*(CASE WHEN [Period Indicator]='N'
THEN DATEDIFF(d,'1900-01-01',[Period End])
ELSE DATEDIFF(d,'1900-01-01',DATEADD(DAY,-1,[Period End]))
END)) as 'Earn % c',
(1.000000000000000*(DATEDIFF(d,'1900-01-01',DATEADD(DAY,-1,[Period Start]))))as 'Earn % d'
into #temptemp2
from #temptemp1