2014-11-18 3 views
0

두 개의 필드 날짜와 이름이있는 테이블을 만들었습니다.Postgres Throws Error 하나의 쿼리에서 두 개의 select 문을 사용하는 경우

insert into temptable with date as (select date from generate_series('2014-04-01', 
    '2014-04-30', '1 day'::interval) as date),name as (select name from table12 where id=1912) 

을하지만 쿼리와 같은 오류를 반환 :

ERROR: syntax error at end of input 
LINE 3: ... date),name as (select name from table12 where id=1912) 
                   ^

문제가 함께 거기에 다음과 같이 나는 삽입 다른 tables.my 쿼리에서 두 개의 SELECT 문에 값을 삽입하는 성명을 작성했습니다 질문? 쿼리에서 '함께'사용하는 것이 올바른 방법입니까?

답변

0

당신은 공통 테이블 식을 선택해야합니다

이 전에 삽입 공통 테이블 표현합니다 (with 부분) 을 넣어 : 다른 방법으로

insert into temptable 
with date as (
    select date 
    from generate_series('2014-04-01','2014-04-30', '1 day'::interval) as date 
), 
name as (
    select name 
    from table12 
    where id=1912 
) 
select * 
from date 
    cross join name; 

을, 당신은 넣을 수 있습니다 삽입물 앞의 CTE (개인적으로 선호) :

with date as (
    select date 
    from generate_series('2014-04-01','2014-04-30', '1 day'::interval) as date 
), 
name as (
    select name 
    from table12 
    where id=1912 
) 
insert into temptable 
select * 
from date 
    cross join name; 

두 부분을 어떻게 원하는지 잘 모르겠습니다. CTE가 연결될 수 있습니다.