Solved

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

Posted on 2006-06-24
2
2,539 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

624 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