0
請在下面找到我的選擇。這個選擇的結果如下:使用CTE結果添加額外的行至選擇
fecha1 columna_1 columna_2
09.07.2014 10.00000000 3.00000000
10.07.2014 12.20000000 5.06000000
11.07.2014 15.56600000 9.26240000
12.07.2014 20.18864000 27.35320192
15.07.2014 28.60184480 22.85988716
我的問題是:我想添加額外的行到下面的選擇結果。該行應包括:
fecha1 =有一天,第一天之前在 「fecha1」 列
columna_1 = 0
columna_2 = 0
我能做到這一點表示?
這是實際的選擇:
DECLARE @sales TABLE
(codigo VARCHAR(10) NOT NULL,
fecha DATE NOT NULL,
diferencia NUMERIC(10, 2) NOT NULL);
INSERT INTO @sales(codigo, fecha, diferencia)
VALUES ('q', '20140708', 51),
('q', '20140712', 0.03),
('q', '20140712', 0.033),
('q', '20140712', 0.04),
('q', '20140811', 0.03),
('q', '20140812', 0.01),
('q', '20140815', 0.05),
('q', '20140815', 0.06),
('q', '20140815', 0.02),
('q', '20140114', 0.07),
('q', '20140714', 0.024),
('q', '20140714', 0.024),
('x', '20140709', 0.025),
('x', '20140710', 0.016),
('x', '20140711', 0.036),
('x', '20140712', 0.023),
('x', '20140712', 0.035),
('x', '20140715', 0.057),
('c', '20140709', 0.097),
('c', '20140710', 0.0197),
('c', '20140711', 0.0297),
('c', '20140712', 0.0397),
('c', '20140715', 0.071);
;with ctesource as
(
select
codigo,diferencia,
fecha,
sum(log(1e0 + diferencia)) over (partition by codigo order by fecha rows unbounded preceding) as LogAssetValue
from
@sales
WHERE codigo IN ('c', 'x') and fecha > DATEADD(month, -24, getdate())
)
select convert(varchar, fecha, 104) AS fecha1,
CAST(SUM(Case codigo when 'c' then (exp(LogAssetValue)-1)*100 ELSE 0 END) AS numeric(16, 8)) as columna_1,
CAST(SUM(Case codigo when 'x' then (exp(LogAssetValue)-1)*100 ELSE 0 END) AS numeric(16, 8)) as columna_2
from ctesource
GROUP BY fecha
order by fecha;
非常感謝!!!!!!