Get sales average for a given month

Asked

Viewed 96 times

0

I would like to take the average sales of the month by product description and not by date with day, month and year, but I don’t know how to get.

SELECT AVG(vend_qtde) AS qtde_vendas from tb_vendas WHERE esto_descricao like '% Saco pedra %' and vend_data like '% 03/01/2018 %' GROUP BY esto_descricao;

Script from my database:

CREATE TABLE tb_vendas
(
    vend_cod serial NOT NULL,
    esto_descricao character(30) NOT NULL,
    vend_preco double precision,
    vend_qtde integer,
    vend_nome_cliente character(35),
    vend_cep character(20),
    vend_bairro character(25),
    vend_endereco character(38),
    vend_estado character(20),
    vend_uf character(13),
    vend_data character(20),
    vend_hora character(20),
    CONSTRAINT tb_vendas_pkey PRIMARY KEY (esto_descricao, vend_cod)
)

Records:

inserir a descrição da imagem aqui

1 answer

2


Do not use 'like' for dates, use between or larger and smaller than. Ex.:

select avg(a.vend_qtde) from tb_vendas a where a.esto_descricao like '%Saco pedra%' and vend_data between '2018-01-01' and '2018-01-30'

or

select avg(a.vend_qtde) from tb_vendas a where a.esto_descricao like '%Saco pedra%' and vend_data >= '2018-01-01' and vend_data <= '2018-01-30'

I also advise you to leave the "esto_descricao" field and any other variable text such as VARCHAR type and not CHARACTER. Varchar it uses only the required bytes, charcater will fill the 35 "spaces" even if you use only 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.