Results 1 to 10 of 10

Thread: [2005] SQL ROWNUMBER and DISTINCT

  1. #1

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    [2005] SQL ROWNUMBER and DISTINCT

    Hi! I am creating a report using report viewer. I can already display the desired report except that I would like to add numbering beside my records.
    Example:
    1. Doe John M. Male
    2. Tuesday Mark Male
    1. Park Mark Female
    2. Stew Lindsey Female

    I saw this code
    Code:
    SELECT ROW_NUMBER() OVER (PARTITION BY Gender ORDER BY Age) AS [Partition by Gender], 
           FirstName, 
           Age,
           Gender 
      FROM Person
    I have this code in my app
    Code:
    SELECT DISTINCT (IDNo), LastName, FirstName, MiddleName, Gender FROM table1
    I tried to combine it like this
    Code:
    SELECT ROW_NUMBER() OVER (PARTITION BY Gender ORDER BY LastName) AS ROWID,DISTINCT (IDNo), LastName, FirstName, MiddleName, Gender FROM table1
    but it's not working.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] SQL ROWNUMBER and DISTINCT

    When you use the DISTINCT key word it needs to immediately follow the SELECT key word. The thing is though, all your rows are going to be distinct if they include a row number. What you'll need to do is perform one query to get your distinct data, then perform another query on that and add the row number.

    That said, the DISTINCT key word applies to EVERY field in the row. Why would you have more than one row with the same IDNo?

    I should also point out that this is an SQL-specific question and has nothing to do with VB.NET. As such it belongs in the Database Development forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] SQL ROWNUMBER and DISTINCT

    Code:
    SET NOCOUNT ON
    
    DECLARE @RowSet TABLE (RowNumber INT IDENTITY(1,1),  LastName VARCHAR(200), FirstName VARCHAR(200), MiddleName VARCHAR(200), Gender CHAR)
    
    INSERT @RowSet 
    SELECT IDNo, LastName, FirstName, MiddleName, Gender FROM table1
    
    
    SELECT RowNumber,  IDNo, LastName, FirstName, MiddleName, Gender FROM @RowSet ORDER BY RowNumber
    
    SET NOCOUNT OFF

  4. #4

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [2005] SQL ROWNUMBER and DISTINCT

    Quote Originally Posted by jmcilhinney
    That said, the DISTINCT key word applies to EVERY field in the row. Why would you have more than one row with the same IDNo?
    Because my table contains different subject enrolled by the student having that IDNo.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] SQL ROWNUMBER and DISTINCT

    But if this table, as it seems, is just about the students, you should be normalizing your data structure and placing the subjects they've enrolled in in another table. That can be a one to many relationship, with the subjects-enrolled-in table having multiple rows for the same student ID.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] SQL ROWNUMBER and DISTINCT

    Quote Originally Posted by mendhak
    But if this table, as it seems, is just about the students, you should be normalizing your data structure and placing the subjects they've enrolled in in another table. That can be a one to many relationship, with the subjects-enrolled-in table having multiple rows for the same student ID.
    Quite so. You might have a Student table with StudentID and Name columns, a Subject table with SubjectID and Name columns and then a SubjectEnrollment table with SubjectID and StudentID columns. Each column in the SubjectEnrollment table is foreign key and both together form the primary key. Any additional information about either Subject or Student goes in just the table that relates to that entity and the many-to-many relationship embodied by the SubjectEnrollment table never has more than the two columns.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [2005] SQL ROWNUMBER and DISTINCT

    I can create another table for that and have a relationship between tables. with my existing setup, is it possible for me just to add the row_number by using distinct and row_number in my SQL?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2005] SQL ROWNUMBER and DISTINCT

    @simplyme

    When you have

    Code:
    SELECT DISTINCT (IDNo), LastName, FirstName, MiddleName, Gender FROM table1
    I notice you have ()'s around IDNO.

    Why do you have these parenthesis?

    Do you think that DISTINCT() is a function that contains a field?

    As has been said DISTINCT is simply an optional keyword for SELECT

    SELECT {TOP|DISTINCT} field1, field2 FROM...

    Having all those fields duplicated in the table - IDNo, LastName, FirstName and so on - is bad table design - which leads to problems just like this.

    You need two queries to accomplish this.

    If you want to use these new SQL 2005 RANKING and NUMBERING features then put the SELECT DISTINCT in a subquery and SELECT FROM that.

    Code:
    Select ROW_NUMBER() OVER (PARTITION BY XYZ.Gender ORDER BY XYZ.LastName) AS ROWID
      ,XYZ.IDNo, XYZ.LastName, XYZ.FirstName, XYZ.MiddleName, XYZ.Gender 
      FROM (SELECT DISTINCT IDNo, LastName, FirstName, MiddleName, Gender FROM table1) XYZ
    Last edited by szlamany; May 9th, 2008 at 04:44 AM. Reason: Needed to remove those bogus ()'s around by sub-query example.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [2005] SQL ROWNUMBER and DISTINCT

    Thread moved to Database Development forum - which is where SQL questions belong

  10. #10

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [2005] SQL ROWNUMBER and DISTINCT

    i'll try this one zslamany and be back for any feedback.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

Posting Permissions

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



Click Here to Expand Forum to Full Width