0
I’m having a hard time getting one working select
where to bring the amount of people who work in a enterprise, banded age, age and sex.
I did the example below, but so far I could not make it work, accuses error in alias.
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 Guys the above query is working normally, but the way this one is calculating the direct age (I was saving the age not the date of birth), I changed the field to date in the database and now I’m saving the date of birth, I need you to bring in the number of people I’ve broken up by the company, age based on date of birth, compare the age range I defined and the gender. I tried the query below but it’s 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 are 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
Friend, schemed on http://sqlfiddle.com/ to facilitate SELECT understanding and testing.
– Emerson JS
Define how the data is and how you want to obtain it, in any case search by CASE , GROUP BY and DATETIME FUNCTIONS.
– Motta
What are the age ranges you want to catch?
– Euler01
Euler01, in this example I want people between 29 to 33 years
– Fabio
Emerson, I didn’t know this sqlfiddle.com tool, I’m taking a look at it..
– Fabio
Motta, I tried with TIMESTAMPDIFF in the example I just put up, but also this not going.
– Fabio