Search only the smallest number of each letter

Asked

Viewed 427 times

4

How do I search for only the smallest number of each letter, in Sql server?

My table:

inserir a descrição da imagem aqui

Expected result:

A - 1

B - 2

C - 1

D - 1

E - 3

1 answer

6

You can use the function Minwith the function of aggregation Group By.

I used the Script below for tests:

CREATE TABLE [dbo].[TesteLetra](
    [letra] [varchar](1) NULL,
    [numero] [int] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO
insert into TesteLetra (letra,numero) values
('A',1)
insert into TesteLetra (letra,numero) values
('B',2)
insert into TesteLetra (letra,numero) values
('C',1)
insert into TesteLetra (letra,numero) values
('D',1)
insert into TesteLetra (letra,numero) values
('D',2)
insert into TesteLetra (letra,numero) values
('D',3)
insert into TesteLetra (letra,numero) values
('E',3)
insert into TesteLetra (letra,numero) values
('E',4)

And my consultation was the following:

select letra,Min(numero) ValorMinimo from TesteLetra
Group By letra

Upshot:

inserir a descrição da imagem aqui

  • This is softer than taking candy from a child. :)

  • It worked, thank you.

  • 1

    @Brupikk good that I could help :), do not forget to mark it as correct because it can help other people too.

  • @Marconi without speaking that he earns 2 posts per accept :)

Browser other questions tagged

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