我正在努力应对这次更新...
我想更新表result
中的列#b
,其中columna_1
列来自下面选择的最后一行。匹配字段为id_car
。
因此,如果您运行下面的选择,则columna_1
的最后一行为42229.56230859
,#b
的唯一记录为:
id_car = 1
result = 42229.56230859
任何人都可以帮忙吗?感谢!!!
CREATE TABLE #b(
id_car int,
result money
)
INSERT INTO #b VALUES (1,0)
CREATE TABLE #f(
id_car int,
fecha datetime,
sales money
)
INSERT INTO #f VALUES (1,'2010-10-31',1.10912)
INSERT INTO #f VALUES (1,'2010-11-30',1.77227)
INSERT INTO #f VALUES (1,'2010-12-31',0.66944)
INSERT INTO #f VALUES (1,'2011-01-31',0.34591)
INSERT INTO #f VALUES (1,'2011-02-28',1.73468)
INSERT INTO #f VALUES (1,'2011-03-31',1.50102)
INSERT INTO #f VALUES (1,'2011-04-30',0.87270)
INSERT INTO #f VALUES (1,'2011-05-31',1.51555)
;with ctesource as
(
select
id_car, fecha, sales,
sum( log( 1e0 + sales ) ) over ( partition by id_car order by fecha rows unbounded preceding) as LogAssetValue
from
#f
WHERE id_car= 1 and fecha > '2010-10-30'
)
select convert(varchar, fecha, 104) AS fecha1, fecha,
CAST(SUM(exp(LogAssetValue)-1)*100 AS numeric(20, 8)) as columna_1
from ctesource
GROUP BY fecha
order by fecha;
答案 0 :(得分:1)
的 SqlFiddleDemo 强>
;with
ctesource as (
SELECT
id_car, fecha, sales,
sum( log( 1e0 + sales ) ) over ( partition by id_car order by fecha rows unbounded preceding) as LogAssetValue,
row_number() over (order by fecha) as rn
FROM
f
WHERE id_car= 1
and fecha > '2010-10-30'
)
SELECT convert(varchar, fecha, 104) AS fecha1,
fecha,
rn,
CAST(SUM(exp(LogAssetValue)-1)*100 AS numeric(20, 8)) as columna_1
FROM ctesource
GROUP BY fecha, rn
HAVING rn = (SELECT max(rn) from ctesource)