How to mount a sub-base with 7 tables?

Asked

Viewed 20 times

0

I want to do a sub-query in my query with 7 tables, but I can only do with two tables.

  • Code
SELECT COUNT(cd_cliente) AS qtd_cliente, 
(SELECT COUNT(cd_funcionario) FROM funcionario) AS qtd_funcionario FROM cliente;
  • Upshot
qtd_cliente | qtd_funcionario
     2             2
  • Remainder of the code

How should I complement this subselect that should return as well qtd_fornecedor, qtd_compra_fornecedor, qtd_produto, qtd_venda, qtd_devolucao

SELECT COUNT(cd_fornecedor) AS qtd_fornecedor FROM fornecedor, SELECT COUNT(cd_itens_compra_fornecedor) AS qtd_compra_fornecedor FROM itens_compra_fornecedor,
SELECT COUNT(cd_produto) AS qtd_produto FROM produto, SELECT COUNT(cd_venda) AS qtd_venda FROM VENDA, SELECT COUNT(cd_devolucao) AS qtd_devolucao FROM devolucao
  • Expected result
qtd_cliente | qtd_funcionario | qtd_fornecedor | qtd_compra_fornecedor | qtde_produto | qtd_venda | qtd_devolucao
     2             2                2                     3                   3            3            3

1 answer

0


Build the subselects correctly:

SELECT COUNT(cd_cliente) AS qtd_cliente, 
(SELECT COUNT(cd_funcionario) FROM funcionario) AS qtd_funcionario,
(SELECT COUNT(cd_fornecedor) FROM fornecedor) AS qtd_fornecedor, 
(SELECT COUNT(cd_itens_compra_fornecedor) FROM itens_compra_fornecedor) AS qtd_compra_fornecedor,
(SELECT COUNT(cd_produto) FROM produto) AS qtd_produto, 
(SELECT COUNT(cd_venda) FROM VENDA) AS qtd_venda, 
(SELECT COUNT(cd_devolucao) FROM devolucao) AS qtd_devolucao  FROM cliente;

I just find it odd to list all these quantities with nothing to link them, but you must have your reasons and requirements.

Browser other questions tagged

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