Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Query question

Query question

Scheduled Pinned Locked Moved Database
databasecomgame-devquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    afronaut
    wrote on last edited by
    #1

    Hey all, I've got data structured like this: ID - Contact - Company - Email 1 - Jeff - Acme - Jeff@Acme.com 2 - Hester - Acme - Hester@Acme.com 3 - Mort - Acme - Mort@Acme.com 4 - Jason - Widget - Jason@Widget.com 5 - Harriet - Widget - Harriet@Widget.com I need to write a query that will return only the first contact for each company, in this case it should return: ---------------------------------------- 1 - Jeff - Acme - Jeff@Acme.com 4 - Jason - Widget - Jason@Widget.com Thanks in advance! *->>Always working on my game, teach me *->>something new. cout << "dav1d\n";

    D A 2 Replies Last reply
    0
    • A afronaut

      Hey all, I've got data structured like this: ID - Contact - Company - Email 1 - Jeff - Acme - Jeff@Acme.com 2 - Hester - Acme - Hester@Acme.com 3 - Mort - Acme - Mort@Acme.com 4 - Jason - Widget - Jason@Widget.com 5 - Harriet - Widget - Harriet@Widget.com I need to write a query that will return only the first contact for each company, in this case it should return: ---------------------------------------- 1 - Jeff - Acme - Jeff@Acme.com 4 - Jason - Widget - Jason@Widget.com Thanks in advance! *->>Always working on my game, teach me *->>something new. cout << "dav1d\n";

      D Offline
      D Offline
      Dr_X
      wrote on last edited by
      #2

      Not Tested but give it a try. SELECT ID, Contact, Company, Email FROM [table] GROUP BY Company HAVING MAX(ID) = ID Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

      M 1 Reply Last reply
      0
      • D Dr_X

        Not Tested but give it a try. SELECT ID, Contact, Company, Email FROM [table] GROUP BY Company HAVING MAX(ID) = ID Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

        M Offline
        M Offline
        miah alom
        wrote on last edited by
        #3

        Just a little correction SELECT ID, Contact, Company, Email FROM [table] GROUP BY Company HAVING MIN(ID) = ID

        J 1 Reply Last reply
        0
        • M miah alom

          Just a little correction SELECT ID, Contact, Company, Email FROM [table] GROUP BY Company HAVING MIN(ID) = ID

          J Offline
          J Offline
          Johny Ng
          wrote on last edited by
          #4

          Nope:suss: something like select * from [table] t where id=(select min(id) from [table] tt where t.company=tt.company) HTH

          M 1 Reply Last reply
          0
          • J Johny Ng

            Nope:suss: something like select * from [table] t where id=(select min(id) from [table] tt where t.company=tt.company) HTH

            M Offline
            M Offline
            miah alom
            wrote on last edited by
            #5

            Check this SELECT * FROM DeleteMe GROUP BY Company HAVING MIN(ID) = ID

            J 1 Reply Last reply
            0
            • M miah alom

              Check this SELECT * FROM DeleteMe GROUP BY Company HAVING MIN(ID) = ID

              J Offline
              J Offline
              Johny Ng
              wrote on last edited by
              #6

              It will nerver work. Read BOL about GROUP BY first before posting.

              M A 2 Replies Last reply
              0
              • J Johny Ng

                It will nerver work. Read BOL about GROUP BY first before posting.

                M Offline
                M Offline
                miah alom
                wrote on last edited by
                #7

                Why dont you try it first then reply. I have already tested it and it works fine. I think you need some lesson on Group By.

                1 Reply Last reply
                0
                • J Johny Ng

                  It will nerver work. Read BOL about GROUP BY first before posting.

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #8

                  well the query by alam_pune looks fine to me as well..... its a nice and simple sql and works fine too.....

                  1 Reply Last reply
                  0
                  • A afronaut

                    Hey all, I've got data structured like this: ID - Contact - Company - Email 1 - Jeff - Acme - Jeff@Acme.com 2 - Hester - Acme - Hester@Acme.com 3 - Mort - Acme - Mort@Acme.com 4 - Jason - Widget - Jason@Widget.com 5 - Harriet - Widget - Harriet@Widget.com I need to write a query that will return only the first contact for each company, in this case it should return: ---------------------------------------- 1 - Jeff - Acme - Jeff@Acme.com 4 - Jason - Widget - Jason@Widget.com Thanks in advance! *->>Always working on my game, teach me *->>something new. cout << "dav1d\n";

                    A Offline
                    A Offline
                    Anonymous
                    wrote on last edited by
                    #9

                    select * from [table] where ID in (select min(ID) from [table] group by company )

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups