Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2003
    Posts
    4

    Unanswered: How to get unique record with columns

    I have result set as the attached in screenshot, where the candidateid gets duplicated, but then the column university and careerchoice column has different values for the same candidate. (the CareerChoice and University comes from different tables and i have used union clause)

    What want is e.g. CandidateId 186 repeats three times, it should be one row, with three columns all filled with the values which are in different rows.
    So that i dont have duplicate candidate ids and i have single row with all column data.

    Any idea? thanks for help
    Attached Thumbnails Attached Thumbnails SQLResult.JPG  

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    What happens if you have 4 rows for a single record? Which of the records do you want to pick?

    This query arbitrarily grabs the "Max" value in each column
    Code:
    SELECT candidateid
         , Max(university) As university
         , Max(careerchoice) As careerchoice
         , Max(company) As company
    FROM   your_table
    GROUP
        BY candidateid
    George
    Home | Blog

  3. #3
    Join Date
    Sep 2006
    Location
    Surrey, UK
    Posts
    995
    Provided Answers: 2
    This problem is tricky to answer without knowing what the source data looks like.

    Is this all coming from one table? If it's the output from a view or sproc that combines multiple tables, what is the structure of the tables and how are they related?
    10% of magic is knowing something that no-one else does. The rest is misdirection.

  4. #4
    Join Date
    Sep 2003
    Posts
    4

    Data comes from multiple table using Stored procedure

    Quote Originally Posted by weejas View Post
    This problem is tricky to answer without knowing what the source data looks like.

    Is this all coming from one table? If it's the output from a view or sproc that combines multiple tables, what is the structure of the tables and how are they related?
    Thanks for the reply,
    Please note the data comes from four different tables, and then it is output of stored procedure, and also note, that SP also use UDF for one column (i.e. career choice) which has used unpivot clause.

    Over all the requirement is complex, but till now, i have manage to get this much records, where single candidates data may have in multiple records (but in different columns), now i want it in single row for each candidate, having data for each column.

    This is where i have stuck.
    Thanks for the interest.

  5. #5
    Join Date
    Sep 2003
    Posts
    4
    Quote Originally Posted by gvee View Post
    What happens if you have 4 rows for a single record? Which of the records do you want to pick?

    This query arbitrarily grabs the "Max" value in each column
    Code:
    SELECT candidateid
         , Max(university) As university
         , Max(careerchoice) As careerchoice
         , Max(company) As company
    FROM   your_table
    GROUP
        BY candidateid
    Thanks for solution, it partially worked, in the sense, if one one university matching record is there, it would work, but it more than 1 university records matches, then the max one only record is selected, it would be better to have comma separated values all all matched records in a single record column.

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Quote Originally Posted by meenxip View Post
    it would be better to have comma separated values all all matched records in a single record column.
    No it would not.

    It would be better to normalise the data. If you have a 1:M relationship then you need to model it this way.

    If you want to DISPLAY the records as a comma separated list then you need to pass the dataset to your front-end / reporting tool and have that do the work for you.
    George
    Home | Blog

  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    For us to solve this problem you need to provide more comprehensive raw data as well as the required output for the sample.
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •