SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Data for each distinct record

    HI folks,

    I am working in MSSQL and trying to pull out

    COURSE_PRODUCT_ID and COURSE_NAME but only the in the first instance of each COURSE_PRODUCT_ID.
    This doesn't work:

    Code:
    SELECT DISTINCT
    Course.COURSE_PRODUCT_ID,
    Course.COURSE_NAME
    FROM Course
    ORDER BY Course.COURSE_NAME
    It returns every record. Can anyone tell me how to get it to only return the first instance
    of each COURSE_PRODUCT_ID?

    Thanks in advance

    Dave

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by davecheet View Post
    Can anyone tell me how to get it to only return the first instance
    of each COURSE_PRODUCT_ID?
    something is seriously amiss

    if you run a DISTINCT on both course_product_id and course_name, and it "doesn't work" to produce only one instance of course_product_id, then this implies that there are course_product_id values that have multiple different course_name values

    your data is b0rked
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by davecheet View Post
    HI folks,

    I am working in MSSQL and trying to pull out

    COURSE_PRODUCT_ID and COURSE_NAME but only the in the first instance of each COURSE_PRODUCT_ID.
    This doesn't work:

    Code:
    SELECT DISTINCT
    Course.COURSE_PRODUCT_ID,
    Course.COURSE_NAME
    FROM Course
    ORDER BY Course.COURSE_NAME
    It returns every record. Can anyone tell me how to get it to only return the first instance
    of each COURSE_PRODUCT_ID?

    Thanks in advance

    Dave
    What do you define as the first instance? Last course entered? First of the day?


Bookmarks

Posting Permissions

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