Hy everyone, I got a problem, I have this table
[BERLIN](
[QUA] [date],
[COST] [decimal](18, 2) ,
[REVEN] [decimal](18, 2),
[COSA] [nvarchar](50)
I'm doing a lot of average, sum and count but now I'm locked with a problem. The table is so populated:
QUA COST REVEN COSA
12/3/12 100,23 NULL SHOES
12/3/12 1543,00 NULL ADVERT
13/3/12 735,45 NULL CAMERA
13/3/12 NULL 1035 SERVICE
......................................................
By this query:
;WITH CTE AS
(
SELECT QUA, SUM(COST) AS COSTI, SUM(IIF(REVEN>0,REVEN,0)) AS RICAVI FROM BERLIN GROUP BY QUA)
SELECT QUA, COSTI-RICAVI as media FROM CTE
I got: date and average
2013-03-121643.232013-03-13299.55
2013-03-142773.70
2013-03-15-872.00
2013-03-161062.50
............................... I
I'd like to build a column with the average of the last ten days and I don't know if I have to try populating this other colum ( maybe using rowcount or the cursor) or if there is a way using the same query but I don't think so, surely I need you a loop here. Any suggestion? Thanks a lot