Solved

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

Posted on 2006-06-24
2
2,524 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
Thanks angelIII ...works perfectly! :-)

Cheers,
Mark
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now