Created
May 20, 2016 21:24
-
-
Save sebawebber/6eb2493d2f6a4a50e506cd5f92f6b5aa to your computer and use it in GitHub Desktop.
Exemplo [bem] básico de modelo pra trabalhar com vendas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- cadastra os produtos | |
| INSERT INTO PRODUTO (id, nome, valor) | |
| SELECT serie, 'Produto ' || serie as nome, serie * 10.0 as valor FROM generate_series(1,20) as serie; | |
| -- Insere uma nota 1 | |
| BEGIN; | |
| -- NOW() retorna a data/hora de agora | |
| INSERT INTO nota (id, data_emissao) | |
| VALUES (1, NOW()); | |
| -- Agora adiciono os produtos | |
| INSERT INTO nota_item (id, nota_id, produto_id, quantidade, valor_total) | |
| VALUES(1, 1, 4, 3, 12.0::NUMERIC); | |
| INSERT INTO nota_item (id, nota_id, produto_id, quantidade, valor_total) | |
| VALUES(2, 1, 5, 3, 15.0::NUMERIC); | |
| COMMIT; | |
| -- Insere uma nota 2 | |
| BEGIN; | |
| -- NOW() retorna a data/hora de agora | |
| INSERT INTO nota (id, data_emissao) | |
| VALUES (2, NOW()); | |
| -- Agora adiciono os produtos | |
| INSERT INTO nota_item (id, nota_id, produto_id, quantidade, valor_total) | |
| VALUES(1, 2, 4, 3, 12.0::NUMERIC); | |
| INSERT INTO nota_item (id, nota_id, produto_id, quantidade, valor_total) | |
| VALUES(2, 2, 1, 3, 3.0::NUMERIC); | |
| COMMIT; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- precisa revisar MESMO | |
| WITH produtos_score AS ( | |
| SELECT | |
| nota_item.produto_id, | |
| COUNT(nota_item.produto_id) as total | |
| FROM nota | |
| JOIN nota_item ON nota.id = nota_item.nota_id | |
| WHERE data_emissao::DATE = NOW()::DATE | |
| GROUP BY nota_item.produto_id | |
| order by total desc | |
| LIMit 1 | |
| ) | |
| SELECT produto_id, produto.nome, total FROM produtos_score | |
| JOIN produto on produto.id = produtos_score.produto_id; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TABLE produto( | |
| id INT PRIMARY KEY, | |
| nome VARCHAR(100), | |
| valor NUMERIC | |
| ); | |
| CREATE TABLE nota ( | |
| id INT PRIMARY KEY, | |
| data_emissao TIMESTAMP NOT NULL | |
| ); | |
| CREATE TABLE nota_item ( | |
| id INT, | |
| nota_id INT REFERENCES nota(id), | |
| produto_id INT REFERENCES produto(id), | |
| quantidade INT, | |
| valor_total NUMERIC, | |
| PRIMARY KEY (id, nota_id) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment