0

I'm trying to create a table using these commands:

SELECT
    data_req,
    count(servico) as qtdeServico,
    count(metodo) as qtdeMetodo,
    consumerid,
    metodo,
    status_metodo,
    servico,
    sum(tempo_req)
INTO
    consolidado
FROM 
    requisicoes AS r
WHERE 
    r.data_req > SELECT MAX(c.data_req) FROM consolidado c  
GROUP BY 
    data_req, consumerid, metodo, status_metodo, servico;

Unfortunately I'm getting an error at the select clause inside where. I'm using postgres.

Any suggestions? Thanks in advance.

2
  • shouldn't the subquery in the where clause be wrapped in brackets? Commented Sep 11, 2018 at 17:26
  • The query doesn't make sense. You seem to be trying to create a table called consolidado while also selecting from that same table. Sample data and desired results are really helpful. Commented Sep 11, 2018 at 17:27

4 Answers 4

1

Your query has numerous idiosyncrasies. The following would be more correct syntax:

create table consolidado as 
    select data_req, count(servico) as qtdeServico, count(metodo) as qtdeMetodo,
           consumerid, metodo, status_metodo, servico,
           sum(tempo_req) as sum_tempo_req
    from requisicoes r
    where r.data_req > (select max(c.data_req) from consolidado c) 
    group by data_req, consumerid, metodo, status_metodo, servico;

This fixes the following errors:

  • Postgres recommends CREATE TABLE AS over SELECT INTO (see here).
  • All columns should be named when creating a table.
  • You need parens around the subquery.

However, the query doesn't make sense. It is creating a table called consolidado and also reading from the table. That is logically troublesome. In addition, qtMetodo and qtdeServico will generally have the same values -- they are in the GROUP BY columns meaning that they are separated by groups.

Sign up to request clarification or add additional context in comments.

Comments

0

Try surrounding the SELECT sentence with "(....)":

SELECT
    data_req,
    count(servico) as qtdeServico,
    count(metodo) as qtdeMetodo,
    consumerid,
    metodo,
    status_metodo,
    servico,
    sum(tempo_req)
    into consolidado
    FROM requisicoes as r
    WHERE 
    r.data_req > (select max(c.data_req) from consolidado c  
    GROUP BY data_req, consumerid, metodo, status_metodo, servico);

Comments

0

Assuming consolidado already exists.. You will need to use INSERT INTO ()... SELECT syntax. The subquery should be inside parentheses as well:

INSERT INTO consolidado (data_req, qtdeServico.. rest of cols)
SELECT
    data_req,
    count(servico) as qtdeServico,
    count(metodo) as qtdeMetodo,
    consumerid,
    metodo,
    status_metodo,
    servico,
    sum(tempo_req)
FROM requisicoes as r
WHERE r.data_req > (select max(c.data_req) from consolidado c)
GROUP BY data_req, consumerid, metodo, status_metodo, servico;

Comments

0

In addition to other's points about CREATE TABLE AS vs SELECT INTO, I would highly suggest you checkout CREATE MATERIALIZED VIEW which sounds like more of what you want..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.