我有这个疯狂的嵌套sql语句,它返回如下内容:
但是,我想要的是在图像中使用此数据返回2列。 col_1 :SUM(avgWithCriteria)/43
和 col_2 :SUM(avgWithoutCriteria)/43
。如何在下面添加我的查询来完成此操作?
SELECT
( ( avgWithCriteria - totalAverage ) / ( ( avgWithCriteria + totalAverage ) / 2 ) ) * 100 as percentDifference,
a.*
FROM
(SELECT
AVG( CASE WHEN 'f' not in ( has_free_parking ) THEN price ELSE null END) as avgWithCriteria,
AVG( CASE WHEN 'f' in ( has_free_parking ) THEN price ELSE null END) as avgWithoutCriteria,
AVG( price ) as totalAverage,
neighbourhood_cleansed
FROM listings
WHERE city_name="berlin"
AND price <= 1000000
AND price >= -1
AND reviews_per_month <= 1000000
AND reviews_per_month >= -1
AND est_monthly_income <= 1000000
AND est_monthly_income >= -1
GROUP BY neighbourhood_cleansed ) a;
答案 0 :(得分:1)
试试这个: -
SELECT percentDifference,avgWithCriteria,avgWithoutCriteria,totalAverage,neighbourhood_cleansed,
(col1_1/43) as col1,(col2_2/43) as col2
from
(
SELECT a.*,SUM(avgWithCriteria) as col1_1,SUM(avgWithoutCriteria) as col2_2
FROM
(
SELECT
( ( avgWithCriteria - totalAverage ) / ( ( avgWithCriteria + totalAverage ) / 2 ) ) * 100 as percentDifference,
a.*
FROM
(SELECT
AVG( CASE WHEN 'f' not in ( has_free_parking ) THEN price ELSE null END) as avgWithCriteria,
AVG( CASE WHEN 'f' in ( has_free_parking ) THEN price ELSE null END) as avgWithoutCriteria,
AVG( price ) as totalAverage,
neighbourhood_cleansed
FROM listings
WHERE city_name="berlin"
AND price <= 1000000
AND price >= -1
AND reviews_per_month <= 1000000
AND reviews_per_month >= -1
AND est_monthly_income <= 1000000
AND est_monthly_income >= -1
GROUP BY neighbourhood_cleansed ) a
) a
GROUP BY percentDifference,avgWithCriteria,avgWithoutCriteria,totalAverage,neighbourhood_cleansed
) a;
答案 1 :(得分:0)
SELECT
( ( avgWithCriteria - totalAverage ) / ( ( avgWithCriteria + totalAverage ) / 2 ) ) * 100 as percentDifference,
SUM(avgWithCriteria)/43 AS col_1,
SUM(avgWithoutCriteria)/43 AS col_2,
a.*
FROM
(SELECT
AVG( CASE WHEN 'f' not in ( has_free_parking ) THEN price ELSE null END) as avgWithCriteria,
AVG( CASE WHEN 'f' in ( has_free_parking ) THEN price ELSE null END) as avgWithoutCriteria,
AVG( price ) as totalAverage,
neighbourhood_cleansed
FROM listings
WHERE city_name="berlin"
AND price <= 1000000
AND price >= -1
AND reviews_per_month <= 1000000
AND reviews_per_month >= -1
AND est_monthly_income <= 1000000
AND est_monthly_income >= -1
GROUP BY neighbourhood_cleansed ) a;