我有两个父表,如下所示
CREATE TABLE GISD.CUSTOMERS
(CUSTOMER_ID INTEGER NOT NULL,
FIRST_NAME VARCHAR (15) NOT NULL,
SURNAME VARCHAR (20) NOT NULL,
DATE_OF_BIRTH DATE NOT NULL,
HOUSE_NUMBER VARCHAR (5) NOT NULL
POST_CODE VARCHAR(8) NOT NULL,
STREET VARCHAR (25) NOT NULL,
TOWN VARCHAR (25) NOT NULL
);
SELECT ADDGEOMETRYCOLUMN('gisd','customers', 'customers_geom', '27700','POINT',2);
和
CREATE TABLE GISD.CINEMAS
(CINEMA_ID INTEGER NOT NULL,
CINEMA_NAME VARCHAR(25) NOT NULL,
ADDRESS_NUMBER INTEGER NOT NULL,
POST_CODE VARCHAR(8) NOT NULL,
STREET VARCHAR (25) NOT NULL,
TOWN VARCHAR (25) NOT NULL,
OPENING_TIME TIME NOT NULL,
CLOSING_TIME TIME NOT NULL
);
SELECT ADDGEOMETRYCOLUMN('gisd','cinemas', 'cinemas_geom', '27700','POLYGON',2);
SELECT ADDGEOMETRYCOLUMN('gisd','cinemas', 'centroid', '27700','POINT',2);
我有一个孩子使用这两个表中的外键:
CREATE TABLE GISD.BOOKING
(BOOKING_ID INTEGER NOT NULL,
CUSTOMER_ID INTEGER NOT NULL,
CINEMA_ID INTEGER NOT NULL,
TIME TIME NOT NULL,
DATE DATE NOT NULL,
FILM VARCHAR(50) NOT NULL,
BOOKING_METHOD VARCHAR (15) NOT NULL,
BOOKING_FEE NUMERIC NOT NULL -- Numeric is suggested by postgresql.org for currency
TICKET_PRICE NUMERIC NOT NULL
);
是否有一种方法可以让我获取一个独特的预订ID并参考客户几何和电影几何来计算ST_Distance?我假设某种嵌套查询可以做到这一点,但我没有运气?
欢呼声
更新(来自评论)
我尝试过以下代码:
SELECT (ST_DISTANCE(
(SELECT centroid
FROM GISD.CINEMAS
INNER JOIN GISD.BOOKING ON CINEMAS.CINEMA_ID=BOOKING.CINEMA_ID
),(
SELECT customers_geom
FROM GISD.CUSTOMERS
INNER JOIN GISD.BOOKING ON CUSTOMERS.CUSTOMER_ID=BOOKING.CUSTOMER_ID
)
))
但是得到一个错误,说'用作表达式的子查询返回了多行'任何想法如何绕过这个?理想情况下,我希望它返回每个预订ID的距离
答案 0 :(得分:1)
使用主键/外键和LOWERCASED名称更正架构。
DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp;
SET search_path=tmp;
CREATE TABLE tmp.customers
( customer_id INTEGER NOT NULL PRIMARY KEY
, first_name VARCHAR (15) NOT NULL
, surname VARCHAR (20) NOT NULL
, date_of_birth DATE NOT NULL
, house_number VARCHAR (5) NOT NULL
, post_code VARCHAR(8) NOT NULL
, street VARCHAR (25) NOT NULL
, town VARCHAR (25) NOT NULL
);
SELECT addgeometrycolumn('tmp','customers', 'customers_geom', '27700','POINT',2);
-- and
CREATE TABLE tmp.cinemas
( cinema_id INTEGER NOT NULL PRIMARY KEY
, cinema_name VARCHAR(25) NOT NULL
, address_number INTEGER NOT NULL
, post_code VARCHAR(8) NOT NULL
, street VARCHAR (25) NOT NULL
, town VARCHAR (25) NOT NULL
, opening_time TIME NOT NULL
, closing_time TIME NOT NULL
);
SELECT addgeometrycolumn('tmp','cinemas', 'cinemas_geom', '27700','POLYGON',2);
SELECT addgeometrycolumn('tmp','cinemas', 'centroid', '27700','POINT',2);
-- I have a junction table between these tables as such:
CREATE TABLE tmp.BOOKING
( booking_id INTEGER NOT NULL PRIMARY KEY
, customer_id INTEGER NOT NULL REFERENCES tmp.customers (customet_id)
, cinema_id INTEGER NOT NULL REFERENCES tmp.cinemas (cinema_id)
, zdatetime timestamp NOT NULL
, film VARCHAR(50) NOT NULL
, booking_method VARCHAR (15) NOT NULL
, booking_fee NUMERIC NOT NULL
, ticket_price NUMERIC NOT NULL
);
三向连接的骨架:
SELECT bo.booking_id, bo.zdatetime, bo.film
, ci.cinemas_geom
, ci.centroid
, cu.customers_geom
FROM booking bo
JOIN customers cu ON cu.customer_id = bo.customer_id
JOIN cinemas ci ON ci.cinema_id = bo.cinema_id
;
现在,只需添加函数调用,使用骨架的结果作为函数参数(我不是那么流利的GIS,这只是演示语法的一个例子):
SELECT bo.booking_id, bo.zdatetime, bo.film
, st_distance(ci.centroid , cu.customers_geom ) AS the_distance
FROM booking bo
JOIN customers cu ON cu.customer_id = bo.customer_id
JOIN cinemas ci ON ci.cinema_id = bo.cinema_id
;