How do I perform an SQL SELECT statement which returns data from clients according to their type, BUT depending on the type, one query has more columns than other, thus i can't do an UNION command, even though puting one more null column to the statement as they would have the same number of columns.
the key fields P.PesTpP 'Tipo' is the type of client, which can will return
I tried to do something like this but it returns this error:
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
select P.PesCod 'Codigo', P.PesNom 'Nome Cliente', P.PesNomRes 'Nome Resumido', P.PesEMl 'E-mail', pt.pestel 'Telefone', e.PesEnd 'Endereco', e.PesEndNum 'Numero', e.PesEndCmp 'Logradouro', e.PesBai 'Bairro', e.PesCep 'CEP', e.PesCidCod 'IBGE', P.PesTpP 'Tipo',
j.jurCod 'CNPJ'
from PesCad P, cptcli c, PESTEL pt, PesEnd e,
PESJUR J
where P.PesCod = c.CliCod and P.PesCod = pt.PesCod and P.PesCod = e.PesCod and p.PesCod = j.JurCod and P.PesTpP = 'J'
UNION ALL
select P.PesCod 'Codigo', P.PesNom 'Nome Cliente', P.PesNomRes 'Nome Resumido', P.PesEMl 'E-mail', pt.pestel 'Telefone', e.PesEnd 'Endereco', e.PesEndNum 'Numero', e.PesEndCmp 'Logradouro', e.PesBai 'Bairro', e.PesCep 'CEP', e.PesCidCod 'IBGE', P.PesTpP 'Tipo', F.FisRGNum 'RG', f.FisCPF'CPF'
from PesCad P, cptcli c, PESTEL pt, PesEnd e,
PESFIS F
where P.PesCod = c.CliCod and P.PesCod = pt.PesCod and P.PesCod = e.PesCod and p.PesCod = F.FisCod and P.PesTpP = 'F'