Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL2005: DISTINCT with ROW_NUMBER() ...multiple results

Posted on 2006-06-24
2
Medium Priority
?
2,544 Views
Last Modified: 2008-02-01
Hi all

I had this query working when I used default paging in my ASP.NET datagrids

SELECT
      DISTINCT APE.*
FROM
      eHR_Accred_Practice_Example APE INNER JOIN
      eHR_Accred_Principle AP ON APE.principle = AP.principle_id INNER JOIN
      eHR_Accred_Quality_Area AQA ON AP.quality_area = AQA.area_id
WHERE
      APE.org_code = '000037'
      AND AQA.area_abbr = 'QA2'


 ... but now I want to use the new SQL2005 row_number() feature and use custom paging in my ASp.NET datagrids to only return a set number of results (Example results 0 - 10 page 1, 10-20 page 2 etc.).

So I wrote this and I can't seem to integrate the 'DISTINCT' in anywhere to stop the query returning multiple results of exactly the same thing (minus the RowNum of course ...that's incrementing perfectly as it should):

SELECT
      RowNum,  
      example_id,  
      date_of_exam,  
      principle,
      description,  
      example,
      notes,  
      org_code
FROM
      (
      SELECT
            ROW_NUMBER() OVER(ORDER BY eHR_Accred_Practice_Example.example_id) as RowNum,  
            eHR_Accred_Practice_Example.example_id As example_id,  
             eHR_Accred_Practice_Example.date_of_exam As date_of_exam,  
             eHR_Accred_Practice_Example.principle As principle,
             eHR_Accred_Practice_Example.description As description,  
             eHR_Accred_Practice_Example.example As example,
             eHR_Accred_Practice_Example.notes As notes,  
             eHR_Accred_Practice_Example.org_code As org_code      
      FROM
            eHR_Accred_Practice_Example
            LEFT JOIN eHR_Accred_Principle ON eHR_Accred_Practice_Example.principle = eHR_Accred_Principle.principle_id
            LEFT JOIN eHR_Accred_Quality_Area ON eHR_Accred_Principle.quality_area = eHR_Accred_Quality_Area.area_id
      WHERE
            eHR_Accred_Practice_Example.org_code = '000037'                  
            AND eHR_Accred_Quality_Area.area_abbr = 'QA2'
      ) as TableInfo
WHERE RowNum BETWEEN 0 AND 10

THAT query returns these multiple results:

1,      000033,      NULL,      000021,      '', '', '',      000037
2,      000033,      NULL,      000021,      '', '', '',      000037
3,      000033,      NULL,      000021,      '', '', '',      000037
4,      000033,      NULL,      000021,      '', '', '',      000037
... etc

I need the results to be unique ...as in only show me ONE of the example_ids 000033, then the next like so:
1,      000033,      NULL,      000021,      '', '', '',      000037
2,      000034,      NULL,      000021,      'testing', '', '',      000037

 ...can anyone help please?


Here's my schema for all the tables mentioned above:

CREATE TABLE [dbo].[eHR_Accred_Practice_Example](
      [example_id] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [date_of_exam] [datetime] NULL,
      [principle] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [description] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [example] [varchar](1000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [notes] [varchar](1000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [org_code] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [PK_eHR_Accred_Practice_Example] PRIMARY KEY CLUSTERED
(
      [example_id] ASC,
      [org_code] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[eHR_Accred_Principle](
      [principle_id] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [principle_name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [principle_description] [varchar](1000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [quality_area] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [org_code] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [isDefault] [varchar](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_eHR_Accred_Principle] PRIMARY KEY CLUSTERED
(
      [principle_id] ASC,
      [org_code] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[eHR_Accred_Quality_Area](
      [area_id] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [area_name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [area_abbr] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [org_code] [varchar](6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [isDefault] [varchar](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_eHR_Accred_Quality_Area] PRIMARY KEY CLUSTERED
(
      [area_id] ASC,
      [org_code] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


Many thanks :-)
Mark
0
Comment
Question by:msiedle
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16974364
any better with this:

SELECT
     RowNum,  
     example_id,  
     date_of_exam,  
     principle,
     description,  
     example,
     notes,  
     org_code
FROM
( SELECT ROW_NUMBER() OVER(ORDER BY example_id) as RowNum,  *
   FROM
     (
     SELECT DISTINCT
          eHR_Accred_Practice_Example.example_id As example_id,  
           eHR_Accred_Practice_Example.date_of_exam As date_of_exam,  
           eHR_Accred_Practice_Example.principle As principle,
           eHR_Accred_Practice_Example.description As description,  
           eHR_Accred_Practice_Example.example As example,
           eHR_Accred_Practice_Example.notes As notes,  
           eHR_Accred_Practice_Example.org_code As org_code    
     FROM
          eHR_Accred_Practice_Example
          LEFT JOIN eHR_Accred_Principle ON eHR_Accred_Practice_Example.principle = eHR_Accred_Principle.principle_id
          LEFT JOIN eHR_Accred_Quality_Area ON eHR_Accred_Principle.quality_area = eHR_Accred_Quality_Area.area_id
     WHERE
          eHR_Accred_Practice_Example.org_code = '000037'              
          AND eHR_Accred_Quality_Area.area_abbr = 'QA2'
  ) as Intermediate
 ) as TableInfo
WHERE RowNum BETWEEN 0 AND 10
0
 
LVL 2

Author Comment

by:msiedle
ID: 16980543
Thanks angelIII ...works perfectly! :-)

Cheers,
Mark
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question