Hello,
I am in the following situation:
I need to select the amount of care of people of some age groups and of those who are male and female.
I have the following tables:
cadastro (id, natendimento, data, sexo, fokfaixaetaria)
and faixaetaria (id, nomefaixaetaria)
the table registry has the column array that is the foreign key of the registry. I currently have the following query:
CREATE PROCEDURE [dbo].[SELECIONAR_NUMERO_ATENDIMENTOS]
@DATAINICIO DATETIME,
@DATAFIM DATETIME
AS
SELECT
faixaetaria.nomefaixaetaria AS FaixaEtaria,
COUNT(case when sexo = 'Masculino' then 1 end) AS Masculino,
COUNT(case when sexo = 'Feminino' then 1 end) AS Feminino,
COUNT(fokfaixaetaria) AS Total
from cadastro
left join faixaetaria on faixaetaria.id = cadastro.fokfaixaetaria
WHERE data_dte_cadastro BETWEEN @DATAINICIO AND @DATAFIM
GROUP BY
faixaetaria.nomefaixaetaria
But during the consultation it is not organized according to the age group, that is, I wanted it to come 0 to 3 - so many male so many females and total, 4 to 6 - so many masculine so many feminine and total, 7 to 12 - so many male so many female and total, everything in sequence. That does not happen he groups according to what comes first from my registration table. I believe that there should be an order by only that I am not able to put it.
Table list:
QueryExecution: