I have the following select:
SELECT A.CARRO,
(SELECT TOP 1 VALOR FROM tab2 B WHERE B.DATA <= A.DATA ORDER BY B.DATA DESC) VALOR
FROM tab1 A
In
(SELECT TOP 1 VALOR FROM tab2 B WHERE B.DATA <= A.DATA ORDER BY B.DATA DESC)
will bring me theVALOR
for thatDATA
or the% "latest".
I have tried to do a% of% by% with%, several forms, one of them:
SELECT A.CARRO,
SUM((SELECT TOP 1 VALOR FROM tab2 B WHERE B.DATA <= A.DATA )) VALOR
FROM tab1 A
GROUP BY A.CARRO
But it returns me error:
You can not perform an aggregate function on an expression that contains an aggregate or a subquery.
- Can not really
GROUP BY
if it has a subquery to bring column value? - Would it be because of the "systemic string" that SQL works, or something like that?
- What other ways could you do to get the result?