SQL Create view statement using WITH keyword

我的梦境 提交于 2019-12-25 09:09:09

问题


I am having issues writing this Create view statement in SQL. I want to get personID, first name, and last name from a table for people that go to the University of Colorado (uid = 2). Then I want to use the WITH clause to combine this table with my body_composition table and print out everything in the body_composition table as well. Here is the exact definition of the query I am making.

First, write a query that returns the person’s id (pid), first name (fname) and last name (lname) from all people who are from the people who go to the University of Colorado. Then, place that query in a WITH clause and use it as a common table expression (CTE) to combine the result with the body composition table via an inner join to get the body compositions for people who attend the University of Colorado.

I try and run my create view statement I get this error

ERROR: syntax error at or near "what" LINE 7: WITH what.body_composition as c

Here is my code for this create view statement along with the tables that I am using.

CREATE VIEW withclause AS
SELECT a.pid, a.fname, a.lname
FROM what.person AS a
INNER JOIN what.university AS b
on a.uid = b.uid
WHERE uid = 2
WITH what.body_composition AS c
SELECT *
FROM what.body_composition;

Here are the three tables that I am using

                  Table "what.university"
 Column          |         Type          |                        Modifiers                        
-----------------+-----------------------+--------------------------------------
 uid             | integer               | not null default nextval('university_uid_seq'::regclass)
 university_name | character varying(50) | 
 city            | character varying(50) | 


 Table "what.body_composition"
 Column |  Type   | Modifiers 
--------+---------+-----------
 pid    | integer | not null
 height | integer | not null
 weight | integer | not null
 age    | integer | not null


    Table "what.person"
 Column |         Type          |                      Modifiers                      
--------+-----------------------+-----------------------------------------------
 pid    | integer               | not null default nextval('person_pid_seq'::reg class)
 uid    | integer               | 
 fname  | character varying(25) | not null
 lname  | character varying(25) | not null

回答1:


Based on the problem description I'm pretty sure this is what you want:

CREATE VIEW withclause AS

WITH cte AS (
  SELECT p.pid, p.fname, p.lname
  FROM what.person as p
  INNER JOIN what.university as u
  ON p.uid = u.uid
  WHERE p.uid = 2
)

SELECT cte.pid, cte.fname, cte.lname, c.age, c.height, c.weight
FROM cte
INNER JOIN what.body_composition c on c.pid = cte.pid;

Sample SQL Fiddle (based on Postgres which I'm assuming you're using based on the psql tag).



来源:https://stackoverflow.com/questions/26207028/sql-create-view-statement-using-with-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!