我遇到自联接表的问题,其中group by工作不正常或我的代码出错。
我正在尝试制作一个表格,其中包含住房销售数据,我希望按地区和住房类型分组结果。
到目前为止,这是我所拥有的,它接近我所需要的。
SELECT A1.AREA AREA,
TRUNC((A1.SOLD_PRICE/A1.ASK_PRICE*100), 2) || '%' "ATT / ROW / TWNHOUSE",
TRUNC((A2.SOLD_PRICE/A2.ASK_PRICE*100), 2) || '%' "DETACHED",
TRUNC((A3.SOLD_PRICE/A3.ASK_PRICE*100), 2) || '%' "SEMI-DETACHED"
FROM SALESRECORDS A1, SALESRECORDS A2, SALESRECORDS A3
WHERE A1.AREA = A2.AREA AND A1.AREA = A3.AREA
GROUP BY A1.AREA, A1.HOUSE_TYPE;
这给了我错误ORA-00979: not a GROUP BY expression
。
当我遵循SQL规则,并且我将所有项目添加到group by子句时,我得到的结果类似于order by statement ....
SELECT A1.AREA,
TRUNC((A1.SOLD_PRICE/A1.ASK_PRICE*100), 2) || '%' "ATT / ROW / TWNHOUSE",
TRUNC((A2.SOLD_PRICE/A2.ASK_PRICE*100), 2) || '%' "DETACHED",
TRUNC((A3.SOLD_PRICE/A3.ASK_PRICE*100), 2) || '%' "SEMI-DETACHED"
FROM SALESRECORDS A1, SALESRECORDS A2, SALESRECORDS A3
WHERE A1.AREA = A2.AREA AND A1.AREA = A3.AREA
GROUP BY A1.AREA, A1.SOLD_PRICE, A1.ASK_PRICE, A2.SOLD_PRICE, A2.ASK_PRICE,
A3.SOLD_PRICE, A3.ASK_PRICE;
有人可以告诉我我做错了吗??
答案 0 :(得分:0)
注意: 根据其他信息进行编辑。还修复了原始答案中不匹配的括号。
请求的输出如下所示:
Area Att/Row/Twnhouse Detached Semi-Detached
---- ---------------- -------- -------------
East 20.00% 33.33% 75.12%
West 22.22% 44.44% 55.10%
如果您需要一个组的总体百分比,唯一安全的方法是首先计算总“卖价”和总“卖价”,然后对总数进行计算。为此你需要一个子查询来总结一切。然后外部查询可以进行数学运算。
尝试这样的事情:
SELECT
Area "Area",
ROUND(TownhouseSold / TownhouseAsk * 100, 2) || '%' "Att/Row/Twnhouse",
ROUND(DetachedSold / DetachedAsk * 100, 2) || '%' "Detached",
ROUND(SemiDetachedSold / SemiDetachedAsk * 100, 2) || '%' "Semi-Detached"
FROM (
SELECT
Area,
SUM(CASE WHEN House_Type = 'Att/Row/Twnhouse' THEN Sold_Price END) AS TownhouseSold,
SUM(CASE WHEN House_Type = 'Att/Row/Twnhouse' THEN Ask_Price END) AS TownhouseAsk,
SUM(CASE WHEN House_Type = 'Detached' THEN Sold_Price END) AS DetachedSold,
SUM(CASE WHEN House_Type = 'Detached' THEN Ask_Price END) AS DetachedAsk,
SUM(CASE WHEN House_Type = 'Semi-Detached' THEN Sold_Price END) AS SemiDetachedSold,
SUM(CASE WHEN House_Type = 'Semi-Detached' THEN Ask_Price END) AS SemiDetachedAsk
FROM SalesRecords
GROUP BY Area
)
ORDER BY Area
附录问题重申为“所售房屋的百分比超过要价,按地区划分”。
方法是计算(a)房屋数量和(b)以高于要价的价格销售的数量,以及按地区划分的数量。这是内部查询。它也按房屋类型转弯。外部查询计算百分比。这是:
SELECT
Area "Area",
ROUND(TownhouseOverAsk / TownhouseCount * 100, 2) || '%' "Att/Row/Twnhouse",
ROUND(DetachedOverAsk / DetachedCount * 100, 2) || '%' "Detached",
ROUND(SemiDetachedOverAsk / SemiDetachedCount * 100, 2) || '%' "Semi-Detached"
FROM (
SELECT
Area,
SUM(CASE WHEN House_Type = 'Att/Row/Twnhouse' THEN 1 END) AS TownhouseCount,
SUM(CASE WHEN House_Type = 'Att/Row/Twnhouse' AND Sold_Price > Ask_Price THEN 1 END) AS TownHouseOverAsk,
SUM(CASE WHEN House_Type = 'Detached' THEN 1 END) AS DetachedCount,
SUM(CASE WHEN House_Type = 'Detached' AND Sold_Price > Ask_Price THEN 1 END) AS DetachedOverAsk,
SUM(CASE WHEN House_Type = 'Semi-Detached' THEN 1 END) AS SemiDetachedCount,
SUM(CASE WHEN House_Type = 'Semi-Detached' AND Sold_Price > Ask_Price THEN 1 END) AS SemiDetachedOverAsk,
FROM SalesRecords
GROUP BY Area
)
ORDER BY Area