Solved

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

Posted on 2006-06-24
2
2,536 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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

830 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