在MySQL中,我有這樣的查詢/表AVG()不進行分組
SELECT *
FROM
(
SELECT
id,
(SELECT MY_FUNCTION(id)) AS rating -- Function returns a float
FROM my_table
WHERE
/* Very long WHERE */
) AS ratings
id rating
---------- -------------
1 1.00
2 2.00
3 10.00
4 11.05
5 10.04
現在,我試圖讓avereage評級
SELECT *, AVG(rating)
FROM
(
SELECT
id,
(SELECT MY_FUNCTION(id)) AS rating -- Function returns a float
FROM my_table
WHERE
/* Very long WHERE */
) AS ratings
id rating AVG(rating)
---------- ------------- -------------
1 1.00 6,818
因爲AVG()是一個聚合函數,其他線條被剝離。我想(不喜歡here)保持這樣的線,沒有做另一個選擇my_table
,因爲WHERE太複雜,做兩次。就像這樣:
id rating AVG(rating)
---------- ------------- -------------
1 1.00 6,818
2 2.00 6,818
3 10.00 6,818
4 11.05 6,818
5 10.04 6,818
我也試過這樣:Using SUM() without grouping the results,但我不能讓CROSS JOIN與表的別名或沒有做複雜的WHERE部分兩次工作。
http://dba.stackexchange.com/questions/40130/mysql-and-window-functions –