我有这个大学任务使用来自多个表的SELECT语句创建表,但它不是那么简单......这里的基本信息:
我正在使用2张桌子 -
CITY(city_ID, name);
PERSON(person_ID, name, surname, city_ID);
--city_ID is FK indicating in which city person was born.
现在我的任务是创建新表
STATISTICS(city_ID, city_name, number_of_births);
--number_of_births is basically a count of people born in each city
问题是我必须只使用SELECT语句才能这样做。
我尝试过这样的事情:(我很清楚这不可行,但是为了让你更好地了解我被困的地方)
CREATE TABLE Statistics AS
(SELECT city.city_ID, city.name as "city_name", number_of_births AS
(SELECT COUNT(*) FROM person WHERE person.city_id = city.city_id)
FROM city, person);
答案 0 :(得分:1)
对于SQL Server,您可以执行SELECT * INTO
。像这样:
SELECT
*
INTO Statistics
FROM (
SELECT
city.city_ID,
city.name as "city_name",
(SELECT COUNT(*) FROM person WHERE person.city_id = city.city_id) as 'number_of_births'
FROM city
inner join person on city.city_id = person.city_id
) t1
答案 1 :(得分:0)
(代表问题作者发布)。
好,这真的很混乱。用Oracle方言重写时,Dave Zych的答案是正确的。
CREATE TABLE Statistics AS SELECT * FROM (
SELECT DISTINCT
city.city_ID,
city.name AS "City_name",
(SELECT COUNT(*) FROM person WHERE person.city_ID = city.city_ID) AS "number_of_births"
FROM city INNER JOIN person ON city.city_ID = person.city_ID);