Order by SQL Server under Procedure

Asked

Viewed 782 times

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:

Tabela Faixa Etaria na Ordem

Query execution:

Execução da query acima

inserir a descrição da imagem aqui

  • can send an image of how the query is returning?

  • @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.

1 answer

3


You do not need to create a subquery. SQL Server allows you to include in GROUP BY columns that do not appear in the SELECT statement. Just add the id column in GROUP BY and sort it using the new column included in GROUP BY.

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, faixaetaria.id
ORDER BY faixaetaria.id

In response to the comment, yes is possible. Just for example do

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 faixaetaria 
lEFT JOIN cadastro 
  ON cadastro.fokfaixaetaria = faixaetaria.id
 AND data_dte_cadastro BETWEEN @DATAINICIO AND @DATAFIM 
GROUP BY faixaetaria.nomefaixaetaria, faixaetaria.id
ORDER BY faixaetaria.id

To include in the result set a record with the total you can use the ROLLUP/GROUPING SETS doing the following

CREATE PROCEDURE [dbo].[SELECIONAR_NUMERO_ATENDIMENTOS] 
@DATAINICIO DATETIME,
@DATAFIM DATETIME
AS

SELECT CASE WHEN GROUPING(faixaetaria.nomefaixaetaria) = 1 THEN 'Total geral' ELSE faixaetaria.nomefaixaetaria END 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 faixaetaria 
LEFT JOIN cadastro 
  ON cadastro.fokfaixaetaria = faixaetaria.id
 AND data_dte_cadastro BETWEEN @DATAINICIO AND @DATAFIM 
GROUP BY GROUPING SETS((faixaetaria.nomefaixaetaria, faixaetaria.id),())
ORDER BY ISNULL(faixaetaria.id, 99)
  • your solution is very simple!

  • Would it be possible in the above query to bring all age groups of the faixaetary table even if they do not have information in the registration table? and set 0 for both sexes and total?

  • It did not work it only brings the values that are in the table register.

  • Are you sure? Note that in my second query, I am selecting all the records from the Flash table. The first and second query have a different base: in the second I am selecting everything from the table, regardless of whether it is in the table register or not.

  • Note also that there is no Where clause anymore. This is important to not exclude records from the faixaetary table that do not exist in the registration table.

  • 1

    Thanks worked out! I hadn’t realized the non-existence of the Where clause.

  • But a question, if I wanted to sum the values of the total table and display in a single column only the sum of all the values of the total table how can I do? I’m doing it this way but it’s not working he repeats and brings wrong values: SUM(fokfaixaetaria) AS TotalGeral In the above query as you passed me.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.