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. how to write querey return the unique CustomerId with the first CustomerName ?

how to write querey return the unique CustomerId with the first CustomerName ?

Scheduled Pinned Locked Moved Database
tutorialquestionsales
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.
  • A Offline
    A Offline
    AJ Hoge
    wrote on last edited by
    #1

    hii , i have question and i wont ask this question in form of the example. if we have table named CUSTOMER: CUSTOMER TABLE: CustomerID : Integer (Not a primary Key) CustomerName: TEXT

    And This is the table's data:
    CustomerId | CustomerName
    12 | Jone
    25 | Mark
    25 | Marc
    10 | AJ

    we note that CustomerId number 25 has deffrint spiling of the CustomerName for the same Customer! i need to return the unique CustomerId with the first CustomerName. i write this querey :

    select DISTINCT CustomerID , CustomerName
    From Customer

    but it got me the four records !! the 25 is dublicated twies becuse the spiling of the name are deffrint for the same Customer how to write querey return the unique CustomerId with the first CustomerName? Many Thanks

    I J N I 4 Replies Last reply
    0
    • A AJ Hoge

      hii , i have question and i wont ask this question in form of the example. if we have table named CUSTOMER: CUSTOMER TABLE: CustomerID : Integer (Not a primary Key) CustomerName: TEXT

      And This is the table's data:
      CustomerId | CustomerName
      12 | Jone
      25 | Mark
      25 | Marc
      10 | AJ

      we note that CustomerId number 25 has deffrint spiling of the CustomerName for the same Customer! i need to return the unique CustomerId with the first CustomerName. i write this querey :

      select DISTINCT CustomerID , CustomerName
      From Customer

      but it got me the four records !! the 25 is dublicated twies becuse the spiling of the name are deffrint for the same Customer how to write querey return the unique CustomerId with the first CustomerName? Many Thanks

      I Offline
      I Offline
      il_masacratore
      wrote on last edited by
      #2

      Maybe it ins't the best solution but you can use a group by (CustomerId), then you have to choose wich CustomerName is the choosen (min(CustomerName), max(CustomerName...). Sql: >select CustomerId, max(CustomerName) from Customer group by CustomerId.

      Visit my blog at http://dotnetforeveryone.blogspot.com

      A 1 Reply Last reply
      0
      • A AJ Hoge

        hii , i have question and i wont ask this question in form of the example. if we have table named CUSTOMER: CUSTOMER TABLE: CustomerID : Integer (Not a primary Key) CustomerName: TEXT

        And This is the table's data:
        CustomerId | CustomerName
        12 | Jone
        25 | Mark
        25 | Marc
        10 | AJ

        we note that CustomerId number 25 has deffrint spiling of the CustomerName for the same Customer! i need to return the unique CustomerId with the first CustomerName. i write this querey :

        select DISTINCT CustomerID , CustomerName
        From Customer

        but it got me the four records !! the 25 is dublicated twies becuse the spiling of the name are deffrint for the same Customer how to write querey return the unique CustomerId with the first CustomerName? Many Thanks

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        SELECT
        @customerId as customerId,
        (SELECT TOP 1 customerName FROM Customer WHERE customerId=@customerID ORDER BY customerName) AS customerName

        Whenever your data looks this bad, its a sure-fire signal to clean your data, and ensure your apps only insert clean data.

        A 1 Reply Last reply
        0
        • I il_masacratore

          Maybe it ins't the best solution but you can use a group by (CustomerId), then you have to choose wich CustomerName is the choosen (min(CustomerName), max(CustomerName...). Sql: >select CustomerId, max(CustomerName) from Customer group by CustomerId.

          Visit my blog at http://dotnetforeveryone.blogspot.com

          A Offline
          A Offline
          AJ Hoge
          wrote on last edited by
          #4

          :thumbsup:Ohhh...Yes it is work !! many thanks

          1 Reply Last reply
          0
          • A AJ Hoge

            hii , i have question and i wont ask this question in form of the example. if we have table named CUSTOMER: CUSTOMER TABLE: CustomerID : Integer (Not a primary Key) CustomerName: TEXT

            And This is the table's data:
            CustomerId | CustomerName
            12 | Jone
            25 | Mark
            25 | Marc
            10 | AJ

            we note that CustomerId number 25 has deffrint spiling of the CustomerName for the same Customer! i need to return the unique CustomerId with the first CustomerName. i write this querey :

            select DISTINCT CustomerID , CustomerName
            From Customer

            but it got me the four records !! the 25 is dublicated twies becuse the spiling of the name are deffrint for the same Customer how to write querey return the unique CustomerId with the first CustomerName? Many Thanks

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

            You can even try this declare @tblcustomer table(customerid int,customername varchar(50)) insert into @tblcustomer select 12,'Jone' union all select 25,'Mark' union all select 25,'Marc' union all select 10,'AJ'

            select x.customerid
            ,x.customername
            from
            (
            select
            ROW_NUMBER() over (partition by customerid order by customerid) as rn
            ,customerid
            ,customername
            from @tblcustomer)X(rn,customerid,customername)
            where rn = 1

            Output:

            customerid customername
            10 AJ
            12 Jone
            25 Mark

            :)

            Niladri Biswas

            1 Reply Last reply
            0
            • J J4amieC

              SELECT
              @customerId as customerId,
              (SELECT TOP 1 customerName FROM Customer WHERE customerId=@customerID ORDER BY customerName) AS customerName

              Whenever your data looks this bad, its a sure-fire signal to clean your data, and ensure your apps only insert clean data.

              A Offline
              A Offline
              AJ Hoge
              wrote on last edited by
              #6

              hii ,sorry what do you mean by sure-fire signal ? :^) yes i agree with you that the data is look bad , but i work on a huge data that which is extracted by the Customers them self so i can not edit any thing. :( many thanks

              J 1 Reply Last reply
              0
              • A AJ Hoge

                hii , i have question and i wont ask this question in form of the example. if we have table named CUSTOMER: CUSTOMER TABLE: CustomerID : Integer (Not a primary Key) CustomerName: TEXT

                And This is the table's data:
                CustomerId | CustomerName
                12 | Jone
                25 | Mark
                25 | Marc
                10 | AJ

                we note that CustomerId number 25 has deffrint spiling of the CustomerName for the same Customer! i need to return the unique CustomerId with the first CustomerName. i write this querey :

                select DISTINCT CustomerID , CustomerName
                From Customer

                but it got me the four records !! the 25 is dublicated twies becuse the spiling of the name are deffrint for the same Customer how to write querey return the unique CustomerId with the first CustomerName? Many Thanks

                I Offline
                I Offline
                i j russell
                wrote on last edited by
                #7

                How do you know which is the first CustomerName? Is the 'first' name always the correct one?

                1 Reply Last reply
                0
                • A AJ Hoge

                  hii ,sorry what do you mean by sure-fire signal ? :^) yes i agree with you that the data is look bad , but i work on a huge data that which is extracted by the Customers them self so i can not edit any thing. :( many thanks

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  Member 3018046 wrote:

                  hii ,sorry what do you mean by sure-fire signal

                  It means that it is time to examine why you have 2 customer records with the same Id, but different names.

                  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