1

我有一个自联接表的问题,其中 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 语句的结果......

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;

有人可以告诉我我做错了什么......?

4

1 回答 1

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
于 2013-04-29T17:27:40.190 回答