Search only the smallest number of each letter

4

How do I find only the smallest number of letters in the Sql server?

My table:

Expected result:

A - 1

B - 2

C - 1

D - 1

E - 3
    
asked by anonymous 01.06.2016 / 15:47

1 answer

6

You can use the Min function with the Group By aggregation function.

I used the script below for testing:

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 query was as follows:

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

Result:

    
01.06.2016 / 16:01