I'm having trouble running a select
where you have to bring the amount of people working in a company , separating by age strong>, age and sex .
I've done the example below, but so far I have not been able to make it work, it has an alias error.
qr_consulta.Close;
qr_consulta.SQL.Clear;
qr_consulta.SQL.Add('Select COUNT(*) as total FROM tb_pessoas where ps_idade >= 34 and ps_idade <= 38 AND em_id =:r AND ps_sexo = "Masculino"' );
qr_consulta.Params.ParamByName('r').AsInteger := result ;
qr_consulta.Open();
Edited by Personally the above query is working normally, but the way it is calculating the direct age (was keeping the age not the date of birth), I changed the field to date in the database and now I am saving the date of birth, I need which brings the amount of people separating by the company, age based on date of birth, compare the age range that I set and sex. I tried the query below but it is not working.
result := dbl_consulta.KeyValue;
qr_consulta.Close;
qr_consulta.SQL.Clear;
qr_consulta.SQL.Add('Select COUNT(*) as total FROM tb_pessoas where (SELECT TIMESTAMPDIFF(YEAR, p.ps_idade, CURDATE()) as idade FROM tb_pessoas p) idade >= 29 and idade <= 33 AND em_id =:r AND ps_sexo = "Masculino"' );
The tables look like this:
tb_pessoas
ps_id
ps_nome
ps_idade
ps_sexo
em_id
tb_empresas
em_id
em_nome
em_endereco
em_cnpj
em_telefone