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. select unique record question

select unique record question

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
5 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.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I am using the following code in sql server to get list of mobile numbers and add the country code to it.

    select ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

    I want to do the same but with selecting unque records only.. something like this (but throwing error!)

    select DISTINCT ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

    P L D N 4 Replies Last reply
    0
    • J Jassim Rahma

      Hi, I am using the following code in sql server to get list of mobile numbers and add the country code to it.

      select ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

      I want to do the same but with selecting unque records only.. something like this (but throwing error!)

      select DISTINCT ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      How about a subselect if your database system allows it? SELECT ('973' + contact_moblle) AS mobile_number FROM (SELECT DISTINCT contact_moblle FROM persons WHERE ... ) T Also... "where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8" can be reduced to: "where len(contact_moblle) = 8"

      1 Reply Last reply
      0
      • J Jassim Rahma

        Hi, I am using the following code in sql server to get list of mobile numbers and add the country code to it.

        select ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

        I want to do the same but with selecting unque records only.. something like this (but throwing error!)

        select DISTINCT ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

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

        jrahma wrote:

        something like this (but throwing error!)

        You didn't post an error. I doubt that it'll update the correct field if the dataset is created using distinct values.

        Bastard Programmer from Hell :suss:

        1 Reply Last reply
        0
        • J Jassim Rahma

          Hi, I am using the following code in sql server to get list of mobile numbers and add the country code to it.

          select ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

          I want to do the same but with selecting unque records only.. something like this (but throwing error!)

          select DISTINCT ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

          D Offline
          D Offline
          davidshenba
          wrote on last edited by
          #4

          select ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' GROUP BY contact_moblle order by file_no

          Hope this will help. Check this and let me know the outcome

          -Shenbaga Murugan Paramasivapandian I hate computers and I just mess them up with my code!

          1 Reply Last reply
          0
          • J Jassim Rahma

            Hi, I am using the following code in sql server to get list of mobile numbers and add the country code to it.

            select ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

            I want to do the same but with selecting unque records only.. something like this (but throwing error!)

            select DISTINCT ('973' + contact_moblle) AS mobile_number from persons where contact_moblle is not null and contact_moblle != '' and len(contact_moblle) = 8 and left(contact_moblle, 1) = '3' order by file_no

            N Offline
            N Offline
            Niladri_Biswas
            wrote on last edited by
            #5

            Try this

            ;With CTE AS
            (
            Select
            Rn = Row_Number() Over(Partition By ('973' + contact_moblle) Order By file_no)
            ,('973' + contact_moblle) AS mobile_number
            from persons
            where contact_moblle is not null
            and contact_moblle != ''
            and len(contact_moblle) = 8
            and left(contact_moblle, 1) = '3'
            )

            Select mobile_number
            From CTE
            Where Rn = 1

            Hope this helps

            Niladri Biswas

            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