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. SQL Query Issue

SQL Query Issue

Scheduled Pinned Locked Moved Database
databasecomhelpquestion
8 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.
  • N Offline
    N Offline
    Nayan Ambaliya
    wrote on last edited by
    #1

    Dear all, I have this sort of weird question in terms of sql data and query. Consider the dataset below. I have this results in to a table variable (SQL 2008)

    EmailAddress ID

    abc@test.com 1000
    def@test.com 1001
    ppp@test.com 1002
    iii@test.com 1002
    ttt@test.com 1000

    Now, what I want out of this table variable is the below result

    EmailAddress ID

    abc@test.com 1000
    def@test.com 1001
    ppp@test.com 1002

    What I want is to have first available unique ID and their email address. Can anyone please write a query or direct me. Thanks.

    M V L 3 Replies Last reply
    0
    • N Nayan Ambaliya

      Dear all, I have this sort of weird question in terms of sql data and query. Consider the dataset below. I have this results in to a table variable (SQL 2008)

      EmailAddress ID

      abc@test.com 1000
      def@test.com 1001
      ppp@test.com 1002
      iii@test.com 1002
      ttt@test.com 1000

      Now, what I want out of this table variable is the below result

      EmailAddress ID

      abc@test.com 1000
      def@test.com 1001
      ppp@test.com 1002

      What I want is to have first available unique ID and their email address. Can anyone please write a query or direct me. Thanks.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      There are a number of solutions to this one. Use a sub query select the top 1 from table order by ID Or you can use the Row_Number() and partition grouped and ordered by the ID and an outer query that selected row 1 for each ID. There is also a CTE solution I have seen posted but don't know the details.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • N Nayan Ambaliya

        Dear all, I have this sort of weird question in terms of sql data and query. Consider the dataset below. I have this results in to a table variable (SQL 2008)

        EmailAddress ID

        abc@test.com 1000
        def@test.com 1001
        ppp@test.com 1002
        iii@test.com 1002
        ttt@test.com 1000

        Now, what I want out of this table variable is the below result

        EmailAddress ID

        abc@test.com 1000
        def@test.com 1001
        ppp@test.com 1002

        What I want is to have first available unique ID and their email address. Can anyone please write a query or direct me. Thanks.

        V Offline
        V Offline
        vvashishta
        wrote on last edited by
        #3

        You can do this by using Row_Number functionality with 'Partition By' on ID Column. Then put in Where Claue with condition as RowNum =1.

        - Happy Coding - Vishal Vashishta

        1 Reply Last reply
        0
        • N Nayan Ambaliya

          Dear all, I have this sort of weird question in terms of sql data and query. Consider the dataset below. I have this results in to a table variable (SQL 2008)

          EmailAddress ID

          abc@test.com 1000
          def@test.com 1001
          ppp@test.com 1002
          iii@test.com 1002
          ttt@test.com 1000

          Now, what I want out of this table variable is the below result

          EmailAddress ID

          abc@test.com 1000
          def@test.com 1001
          ppp@test.com 1002

          What I want is to have first available unique ID and their email address. Can anyone please write a query or direct me. Thanks.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          SELECT ID, MIN(EmailAddress)
          FROM TEST
          GROUP BY ID

          Bastard Programmer from Hell :suss:

          P 1 Reply Last reply
          0
          • L Lost User

            SELECT ID, MIN(EmailAddress)
            FROM TEST
            GROUP BY ID

            Bastard Programmer from Hell :suss:

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Are you sure you've got that query right?

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            L 1 Reply Last reply
            0
            • P Pete OHanlon

              Are you sure you've got that query right?

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Is there are reason that you want me to doubt the post?

              Bastard Programmer from Hell :suss:

              P 1 Reply Last reply
              0
              • L Lost User

                Is there are reason that you want me to doubt the post?

                Bastard Programmer from Hell :suss:

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Look at the field in the min as a hint.

                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                L 1 Reply Last reply
                0
                • P Pete OHanlon

                  Look at the field in the min as a hint.

                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I did. He wants the ID's unique, and the first mailadress for that ID. Did I miss something? :)

                  Bastard Programmer from Hell :suss:

                  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