The reason for the error is explained in MySQL documentation in part E.4. Restrictions on Subqueries as:
Subqueries in the FROM clause cannot be correlated subqueries. They are materialized in whole (evaluated to produce a result set) during query execution, so they cannot be evaluated per row of the outer query.
Changing query a bit like the following seems to solve your problem:
SELECT t.CodC, c.NameC, t.Amount, t.Year, t.Week,
IF(t.Week!=@week OR t.Year!=@year, @rank:=1, @rank:=@rank+1) AS rank,
@week:=t.Week, @year:=t.Year
FROM
(SELECT CodC, Sum(Imports) As Amount, Year, Week
FROM Weekly
WHERE Year=2014
GROUP BY CodC, Year, Week
ORDER BY Year DESC, Week DESC, Amount DESC) t
JOIN (SELECT @rank:=0, @week:=-1, @year:=-1) t2 ON 1
LEFT JOIN Companies c ON c.CodC=t.CodC
It basically adds an artificial rank column by checking year and week values on the ordered rows.
Edit
Previous query is given to order by weekly sum because of misunderstanding. Following query will give what you want
SELECT t2.*,
IF(t2.Week!=@week OR t2.Year!=@year, @rank:=1, @rank:=@rank+1) AS rank,
@week:=t2.Week, @year:=t2.Year
FROM
(SELECT t1.*, SUM(w2.Imports) AS cnt
FROM
(SELECT w1.CodC, c.NameC, w1.Week, w1.Year
FROM Weekly w1
LEFT JOIN Companies c on c.CodC=w1.CodC
ORDER BY w1.Year ASC, w1.Week ASC) t1
LEFT JOIN Weekly w2 ON t1.CodC=w2.CodC AND w2.Year=t1.Year AND w2.Week<=t1.Week
GROUP BY t1.CodC, t1.Week, t1.Year
ORDER BY t1.Year DESC, t1.Week DESC, cnt DESC
)t2
JOIN (SELECT @rank:=0, @week:=-1, @year:=-1) t3 ON 1
Fiddle
SELECT * FROM
(
Status,Date, Count(*) RowCount FROM Customer
WHERE Date > (DATE(NOW()) - INTERVAL 7 DAY)
GROUP BY Status,Date
UNION
Status,WEEK(Date) WK,Count(*) RowCount FROM Customer
WHERE Date >= MAKEDATE(YEAR(NOW()),1)
GROUP BY Status,WK
UNION
Status,'YearToDate' YTD,Count(*) RowCount FROM Customer
WHERE Date >= MAKEDATE(YEAR(NOW()),1)
GROUP BY Status,YTD
) A ORDER BY Status,Date;
Best Answer
What you want is called the cumulative sum, you can do something like:
I did not see your additional request for 2 2 for 2014. You can do that by replacing:
...with an expression that creates the whole domain for weeks. It is often a good idea to create a calendar table that you can use to join against to get reports for missing values etc.