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. Need help on query

Need help on query

Scheduled Pinned Locked Moved Database
databasehelpquestion
10 Posts 4 Posters 1 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.
  • K Offline
    K Offline
    krishnan s
    wrote on last edited by
    #1

    Hi, I have a table with username and Registration number. I need to get the Registration number for the distinct usernames from the table.Can it be done using the "DISTINCT" query.?

    A 1 Reply Last reply
    0
    • K krishnan s

      Hi, I have a table with username and Registration number. I need to get the Registration number for the distinct usernames from the table.Can it be done using the "DISTINCT" query.?

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      Maybe. Why not try it and see?

      Bob Ashfield Consultants Ltd

      modified on Monday, April 28, 2008 2:56 AM

      K 1 Reply Last reply
      0
      • A Ashfield

        Maybe. Why not try it and see?

        Bob Ashfield Consultants Ltd

        modified on Monday, April 28, 2008 2:56 AM

        K Offline
        K Offline
        krishnan s
        wrote on last edited by
        #3

        I tried it , Since i can get only the Username , How to select the registration numbers from it? , Is there any other way to frame the query?

        A 1 Reply Last reply
        0
        • K krishnan s

          I tried it , Since i can get only the Username , How to select the registration numbers from it? , Is there any other way to frame the query?

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          1. Post some samples from your table 2. Post your query Then I will be able to help. If you have data like this: Name Reg M. Mouse 1 D Duck 2 then SELECT DISTINCT Name, reg from table will do it.

          Bob Ashfield Consultants Ltd

          K 1 Reply Last reply
          0
          • A Ashfield

            1. Post some samples from your table 2. Post your query Then I will be able to help. If you have data like this: Name Reg M. Mouse 1 D Duck 2 then SELECT DISTINCT Name, reg from table will do it.

            Bob Ashfield Consultants Ltd

            K Offline
            K Offline
            krishnan s
            wrote on last edited by
            #5

            My Table entries are Name Reg M. Mouse 1 D Duck 2 L Lion 3 D Duck 4 M. Mouse 5 SELECT DISTINCT Name, reg from table This query gives the distinct combinations of name and reg not the distinct values for name . Since the combinations are unique all the entries will be returned .I need to get only Reg No's: 1,2,3 Any idea on How to frame the query for it?

            B 1 Reply Last reply
            0
            • K krishnan s

              My Table entries are Name Reg M. Mouse 1 D Duck 2 L Lion 3 D Duck 4 M. Mouse 5 SELECT DISTINCT Name, reg from table This query gives the distinct combinations of name and reg not the distinct values for name . Since the combinations are unique all the entries will be returned .I need to get only Reg No's: 1,2,3 Any idea on How to frame the query for it?

              B Offline
              B Offline
              Blue_Boy
              wrote on last edited by
              #6

              here it is: select distinct [name], (select top 1 a1.reg from myTable as a1 where a1.name = myTable.name order by a1.reg asc) from myTable


              I Love T-SQL

              modified on Monday, April 28, 2008 3:57 AM

              K M 2 Replies Last reply
              0
              • B Blue_Boy

                here it is: select distinct [name], (select top 1 a1.reg from myTable as a1 where a1.name = myTable.name order by a1.reg asc) from myTable


                I Love T-SQL

                modified on Monday, April 28, 2008 3:57 AM

                K Offline
                K Offline
                krishnan s
                wrote on last edited by
                #7

                Thanks for ur help, Its working fine now..

                B 1 Reply Last reply
                0
                • K krishnan s

                  Thanks for ur help, Its working fine now..

                  B Offline
                  B Offline
                  Blue_Boy
                  wrote on last edited by
                  #8

                  You are welcome.


                  I Love T-SQL

                  1 Reply Last reply
                  0
                  • B Blue_Boy

                    here it is: select distinct [name], (select top 1 a1.reg from myTable as a1 where a1.name = myTable.name order by a1.reg asc) from myTable


                    I Love T-SQL

                    modified on Monday, April 28, 2008 3:57 AM

                    M Offline
                    M Offline
                    Marek Grzenkowicz
                    wrote on last edited by
                    #9

                    If you are using SQL Server 2005, you should avoid subselects, because their performance is poor. I'd rather use:

                    SELECT
                    NAME
                    , MIN(REG)
                    FROM dbo.MYTABLE
                    GROUP BY
                    NAME

                    B 1 Reply Last reply
                    0
                    • M Marek Grzenkowicz

                      If you are using SQL Server 2005, you should avoid subselects, because their performance is poor. I'd rather use:

                      SELECT
                      NAME
                      , MIN(REG)
                      FROM dbo.MYTABLE
                      GROUP BY
                      NAME

                      B Offline
                      B Offline
                      Blue_Boy
                      wrote on last edited by
                      #10

                      thanks but your reply is not for me.


                      I Love T-SQL

                      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