以下是我要做的事情的描述: 查看每个活动财产的属性名称,“房间数”,“最优每日价值”和“平均费率”。(最佳每日价值假设所有房间都已满,并按其基本费率收费。)
这是我提出的srcipt但它不起作用,给出了一个错误:
SELECT p.property_name, 'numrooms' AS 'Number of Rooms',
'numrooms' * r.base_rate AS 'Optimal Daily Value',
AVG(r.base_rate) AS 'Average Rate'
FROM property p, room r,(SELECT COUNT(DISTINCT room_id) FROM room AS "numrooms"
GROUP BY p.property_name;
答案 0 :(得分:1)
我猜到了一些列名。
SELECT p.property_name,
COUNT(1) AS [Number of Rooms],
SUM(r.base_rate) AS [Optimal Daily Value],
AVG(r.base_rate) AS [Average Rate]
FROM property p
INNER JOIN room r
ON p.id = r.propertyId
GROUP BY p.property_name
ORDER BY p.property_name
根据您的SQL品牌,列别名可能必须不同。
答案 1 :(得分:0)
根据您提供的详细信息,我相信这样的内容可能与您实际需要的内容接近,但我不确定您是否可以理解查询生成的数据:
SELECT
p.property_name
, numrooms.rooms AS [Number of Rooms]
, numrooms.rooms * r.base_rate AS [Optimal Daily Value]
, AVG(r.base_rate) AS [Average Rate]
FROM property p
, room r
, (SELECT COUNT(DISTINCT room_id) AS rooms FROM room GROUP BY p.property_name) numrooms
但我的建议是发布更多关于房间和属性表中主要/外键的详细信息,以便通过在FROM
之后枚举表来完成的carthesian连接可以更有效地替换JOIN
秒。