List first one in each group

  • I'm having a brain dead day. I used to know how to do this.

    I have several thousand providers in a table. I need to find one record for each distinct value of the specialty.

    Here's the data:

    CREATE TABLE [dbo].TestProviders(

    [ProviderID] [int] IDENTITY(1,1) NOT NULL,

    [Specialty] [nvarchar](50) NULL

    ) ON [PRIMARY]

    INSERT TestProviders (Specialty) VALUES ('Pediatrics')

    INSERT TestProviders (Specialty) VALUES ('Pediatrics')

    INSERT TestProviders (Specialty) VALUES ('Pediatrics')

    INSERT TestProviders (Specialty) VALUES ('Pediatrics')

    INSERT TestProviders (Specialty) VALUES ('Pediatrics')

    INSERT TestProviders (Specialty) VALUES ('Internal Medicine')

    INSERT TestProviders (Specialty) VALUES ('Internal Medicine')

    INSERT TestProviders (Specialty) VALUES ('Internal Medicine')

    INSERT TestProviders (Specialty) VALUES ('Internal Medicine')

    INSERT TestProviders (Specialty) VALUES ('Radiology')

    INSERT TestProviders (Specialty) VALUES ('Radiology')

    INSERT TestProviders (Specialty) VALUES ('Radiology')

    INSERT TestProviders (Specialty) VALUES ('Oncology')

    INSERT TestProviders (Specialty) VALUES ('Oncology')

    I'm looking for the ID of one record with Specialty='Pediatrics', one for 'Internal Medicine'... and it doesn't really matter which one. (I'm mining for test data.)

    Expected output:

    1 Pediatrics

    6 Internal Medicine

    10 radiology

    13 Oncology

  • select specialty, MIN(providerid)

    from TestProviders

    group by specialty

  • Doh! Told you I was brain dead! 😉

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply