1
Hello,
I have 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 registration table has the faixaetaria column that is a foreign faixaetaria key. I am currently with 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 she does not come organized according to age group, ie, I wanted it to come 0 to 3 - so many male both female and total, 4 to 6 - so many feminine and total masculine, 7 to 12 - so many feminine and total masculine, all in sequence. This does not happen it groups according to what comes first from my table register. I believe that there should be an order by only that I’m not able to put it.
Faixaetary table:
Query execution:
can send an image of how the query is returning?
– Jean Gustavo Prates
@Jeangustavoprates the first image is of the table faixaetaria the second is the result of the query I just wish it was in order but not in the order of how they are registered in the table registration but rather for the sake of aesthetics stay in equal order as it is in the table pheasant plant, this facilitates in a report that I will do, but I can’t put order by in sql gives error after group by:
O nome de coluna "faixaetaria.id" é inválido na cláusula ORDER BY porque não está contido em uma função de agregação nem na cláusula GROUP BY.
– Wesley Heron