Solved

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

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Viewers will learn how the fundamental information of how to create a table.

863 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

22 Experts available now in Live!

Get 1:1 Help Now