Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2012
    Posts
    2

    Exclamation Unanswered: Distinct from multiple column(table data)

    Hello,

    I have following table structure and data

    id | Grade 1 | Grade 2 |Grade 3 |
    ---------------------------------
    1 | A | B | C |
    2 | B | A | A |
    3 | C | B | B |
    4 | B | A | D |

    I need to know distinct grades which are being used like A,B,C,D

    I have tried using sub query, eg first I will get distinct from column 1, then 2 n 3 and union will give Distinct grades.

    But I have fear it will degrade sql performance.
    I think there can be more optimal way to achieve this.
    Could you plz share your view.
    Thanks

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    If you have indexes on each of grade1, grade2, grade3 performance may be acceptable. Otherwise I would suggest changing your data model to have id and grade only and allow a single id have multiple grades.
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by webian View Post
    I need to know distinct grades which are being used like A,B,C,D
    Code:
    SELECT grade1 AS grade
      FROM daTable
    UNION
    SELECT grade2
      FROM daTable
    UNION
    SELECT grade3
      FROM daTable
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Apr 2012
    Posts
    2
    Thank you every one for reply..

    Dear r937,
    I have written same query but I was bothered about its performance.

    Thanks once again it-iss.com and r937.

Posting Permissions

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