Created
May 25, 2015 22:51
-
-
Save pnakibar/f1c84834a3b1c1e44740 to your computer and use it in GitHub Desktop.
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 OR REPLACE FUNCTION sexoVendedorPorModelo(varchar(64)) | |
RETURNS character(1) AS | |
$$ | |
DECLARE | |
sex character(1); | |
quantity int := 0; | |
c record; | |
BEGIN | |
FOR c IN | |
SELECT funcionario.sexo AS sexo, count(funcionario.sexo) AS quantity_sold | |
FROM venda | |
INNER JOIN carro ON venda.id_carro = carro.id | |
INNER JOIN funcionario ON venda.cpf_vendedor = funcionario.cpf | |
WHERE carro.modelo = $1 | |
GROUP BY funcionario.sexo | |
LOOP | |
IF c.quantity_sold > quantity THEN | |
quantity := c.quantity_sold; | |
sex := c.sexo; | |
END IF; | |
END LOOP; | |
RETURN sex; | |
END | |
$$ LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment